已删除的文件exe在闪存驱动器中低于特定大小

时间:2014-02-04 19:15:06

标签: vbscript

我有Nathan Rice的代码。它有效,但问题是:

我希望sDirectoryPath ="适用于任何闪存驱动器"

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

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 450Kb
    If oFile.Size < 450000 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

1 个答案:

答案 0 :(得分:0)

'Here we set your global variables. These values
'don't change during the runtime of your script.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")

'We select removable drives from WMI
Set oDrives = oWMIService.ExecQuery( _
    "Select * From Win32_LogicalDisk Where DriveType = 2")

'Loop through the available drives here
For Each oDrive in oDrives
    RecurseFolders oDrive.DeviceID & "\"
Next

'Cleanup
Set oDrives = Nothing



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 450Kb
    If oFile.Size < 450000 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