VBScript删除100kb或更小的文件

时间:2014-02-04 17:02:20

标签: vbscript

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") 

我正在寻找一个脚本,它只会删除文件夹中的exe文件及其400kb或更小的子文件夹。

预先感谢您对此脚本的任何帮助。

1 个答案:

答案 0 :(得分:1)

似乎你从另一个有一些不必要的部分的脚本中复制和粘贴。我删除了它们并添加了必要的缺失部分和一些正确的注释。

'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