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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to delete empty files 1

Status
Not open for further replies.

BradW

IS-IT--Management
Nov 30, 2000
126
US
I have an automated process that pumps out a summary file from a large system every few minutes. If there was no activity, it pumps out a blank (0K) file. I would like to write a script to search a specified directory (p:\archive) and delete all blank files. If there were transactions during the period of time then the file will be at least 2K in size. If I can get a script to do this, then I will schedule it to run every so often to clean out the old garbage. The script would be able to run either on an XP Pro machine or on a W2003 Server.

I have something but it does not seem to work and I am just starting out with scripts. What I have so far is:

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objDIR = GetFolder(sDIR)
sDIR="p:\archive"
lSIZE = 0
uSIZE = 1

Set objDIR = GetFolder(sDIR)
GoSubFolders objDIR

Sub MainSub (objDIR)
For Each efile in ObjDIR.Files
If lSIZE = Null and uSIZE = Null Then
If efile.Size = 0 Then
DelFile efile
End If
ElseIf lSIZE <> Null and uSIZE = Null Then
If efile.Size < lSIZE Then
DelFile efile
End If
ElseIf lSIZE = NULL and uSIZE <> "" Then
If efile.Size > uSIZE Then
DelFile efile
End If
ElseIf lSIZE = uSIZE Then
If efile.SIZE = lSIZE Then
DelFile efile
End If
Else If efile.Size > lSIZE and efile.Size<uSIZE Then
DelFile efile
End If
End IF
Next
End Sub

Sub GoSubFolders (objDIR)
If objDIR <> "\System Volume Information" Then
ListFiles objDIR
For Each eFolder in objDir.SubFolders
Wscript.Echo eFolder
GoSubFolders eFolder
Next
End If
End Sub

Sub DelFile(sFILE)
On Error Resume Next
FSO.DeleteFile sFILE, True
If Err.Number <> 0 Then
Wscript.Echo "Error deleting file: " & sfile
End If
End Sub

Sub ListFiles (objDIR)
For Each efile in objDIR.Files
Wscript.Echo efile
Next
End Sub

Thanks in advance for all help!
 
Hello BradW,

What you have so far is desperately confusing. It doesn't worth to revising it. Question : why you want to walk the subdirectories? Will the process produce summary files all over the places rather than at p:\archive only?

This will delete all files of size 0 byte under p:\archive.
Code:
sdir="p:\archive"

set fso=createobject("scripting.filesystemobject")
if not fso.folderexists(sdir) then
    wscript.echo "Folder " & sdir & " does not exist. Operation aborted."
    set fso=nothing 
    wscript.quit 9
end if
for each ofile in fso.getfolder(sdir).files
    if ofile.size=0 then fso.deletefile ofile.path,true
next
set fso=nothing
regards - tsuji
 
Thanks tsuji! I was trying to work out of a VBS book and I thought it was more complex than it needed to be. Your solution works beautifully and I really appreciate your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top