Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting files based on file size.

Status
Not open for further replies.

Dodge245

Programmer
Oct 9, 2008
2
GB
hi all,

this is my first time on this forum, seems like a good place to ask for a solution to im sure a small problem. All i want to do is create a file, log down some details then when the file reaches a certain size delete and start again. Heres what i have so far.

Code:
Dim objNet, file
On Error Resume Next 

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objNet = CreateObject("WScript.Network")
    If err.Number <> 0 Then 'If error occured then display notice
        Wscript.Echo "Error create Network object" & err.number, err.description
        err.Clear
    End if

If objFSO.FileExists("C:\login.txt") Then

  set file = filesys.GetFile("C:\login.txt")
  if file.Size >= 10240 Then
      WScript.echo "file size =" & file.Size
    objFSO.DeleteFile("C:\login.txt")
  End If
  
End If

If objFSO.FileExists("C:\login.txt") Then

    Set objFile = objFSO.OpenTextFile("C:\login.txt", ForAppending)

    objFile.Write Now
    objFile.Write(" ")
    objFile.Write(objNet.Username)
    objFile.WriteLine

    objFile.Close
    Set objNet = Nothing
Else
    Set objFile = objFSO.CreateTextFile("C:\login.txt")

    Set objFile = objFSO.OpenTextFile("C:\login.txt", ForAppending)

        objFile.Write Now
        objFile.Write(" ")
        objFile.Write(objNet.Username)
        objFile.WriteLine

    objFile.Close
    Set objNet = Nothing
End If

now everything works, apart from this bit

Code:
If objFSO.FileExists("C:\login.txt") Then

  set file = filesys.GetFile("C:\login.txt")
  if file.Size >= 10240 Then
      WScript.echo "file size =" & file.Size
    objFSO.DeleteFile("C:\login.txt")
  End If
  
End If

this deletes the file everytime, however i only want the file to be deleted when it reaches 10kb. can anyone help me with this, i am very much new to vbscript.
 
Replace this:
set file = filesys.GetFile("C:\login.txt")
with this:
Set file = objFSO.GetFile("C:\login.txt")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thank you very much. I feel like an idiot now seeing how easy that solution was lol.

well atleast i can leave my script here, anyone can use it if they want, it just logs the date, time and username, it is to be used in a large internal network to help us identify who may be responsible for damaging equipment(in a school).

again thank you.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top