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!

VBScript to delete files <= "x" kb

Status
Not open for further replies.

chadbi

IS-IT--Management
Nov 24, 2008
14
US
Hello,

I am looking for a script that will delete any file in a folder and it's sub-folders that are 100kb or smaller.

Thanks in advance for any assistance with this script.

 
What have YOU tried so far and where in YOUR code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Here's what I have so far. I can delete the files based on age from today, but I am looking to delete them based on size instead.

==========================================================


iDaysOld = 0
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\Calls\"
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files
'If database log file backups are older than "iDaysOld" days, delete them.
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
Next


'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing

Sub RecurseFolders(oFolder)
set oFileCollection = oFolder.Files
'If database log file backups are older than "iDaysOld" days, delete them.
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
Next
End Sub

Ret=Msgbox("Small Calls Deletion Completed Successfully")
 
Replace this:
If oFile.DateLastModified < (Date() - iDaysOld) Then
with this:
If oFile.Size < 100000 Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you, that worked. Do you happen to know how to include sub-folders as well?
 
So, I have the script below and it is working except when I added the "For Each oSubFolder In oFolder" statement, I am getting the error returned:

Line: 13
Char: 1
Error: Object doesn't support this property or method
Code: 800A01B6
Source: Microsoft VBScript runtime error

Line 13 is the For Each oSubFolder In oFolder statement.

============================================================


Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\calls\"
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files
'If database log file backups are smaller than 100kb, delete them.
For each oFile in oFileCollection
If oFile.Size <= 105000 Then
oFile.Delete(True)
End If
Next

For Each oSubFolder In oFolder
RecurseFolders oSubFolder
Next

'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing

Sub RecurseFolders(oFolder)
set oFileCollection = oFolder.Files
'If database log file backups are smaller than 100kb, delete them.
For each oFile in oFileCollection
If oFile.Size < 105000 Then
oFile.Delete(True)
End If
Next
For Each oSubFolder In oFolder
RecurseFolders oSubFolder
Next
End Sub

Ret=Msgbox("Small Call File Deletion Completed Successfully")

============================================================

Can someone tell me what I am missing for the sub-folder deletion?

Thanks,
Chad
 
For Each oSubFolder In oFolder[!].SubFolders[/!]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
[bigcheeks] Sometimes the most obvious solutions are the ones we miss...thanks for your help...[thumbsup2]
 
One more issue with this script...

I am getting a permission denied error on the:
oFile.Delete(True)

If I run the script on the parent folder, it works fine, but when I try to use it for the sub-folders, I get the error?!? Any suggestions or a way to override this error?
 
Don't know if you figured it out yet or not-

I know on Vista, certain files are read only for whatever reason. If that's the case it will create that error. You can try adding this:
Code:
oFile.Attributes = 0
It will clear any hidden, archived, or read only atribute markers.

If it's not vista, you can also impersonate your security level, which unfortunately I forget how to do.

Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top