Kacimo Benziane
Kacimo Benziane

Reputation: 17

VBScript to delete files that are 100kb or smaller

iDaysOld = 0
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\Nouve\"
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.Size < 400000 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.Size < 100000 Then 
                  oFile.Delete(True)
            End If
      Next      
      End Sub

Ret=Msgbox("Small Calls Deletion Completed Successfully") 

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

Thanks in advance for any assistance with this script.

Upvotes: 0

Views: 2639

Answers (1)

Nathan Rice
Nathan Rice

Reputation: 3111

It seems you copy and pasted from another script that had some unnecessary parts. I've removed them and added the necessary missing pieces with some correct notes.

'Here we set your global variables. These values
'don't change during the runtime of your script.
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\Nouve\"


RecurseFolders sDirectoryPath

Sub RecurseFolders(sFolder)
  'Here we set the oFolder object, note that it's
  'variable scope is within this sub, so you can
  'set it many times and it's value will only be
  'that of the sub that's currently running.
  Set oFolder = oFSO.GetFolder(sFolder)


  'Here we are looping through every file in the
  'directory path.
  For Each oFile In oFolder.Files
    'This just checks for a file size less than 100Kb
    If oFile.Size < 102400 And Right(LCase(oFile.Name),3) = "exe" Then
      oFile.Delete True
    End If
  Next


  'Here we do the recursive bit. We need to loop through
  'each folder in the directory too and call the same
  'sub to ensure we check every folder in the path.
  For Each oFolder In oFolder.SubFolders
    RecurseFolders oFolder.Path
  Next
End Sub

'When calling subs you don't need to set their value
'to a variable name, and you don't use parenthesis.
Msgbox "Small Calls Deletion Completed Successfully"

'Clean up
Set oFSO = Nothing

Upvotes: 2

Related Questions