2016-09-26 74 views
0

我正在尋找同時刪除多個USB驅動程序上的文件夾。在多個USB驅動器上刪除文件夾VBS

sDeleteFolder = "\Test" 
Set oFS = CreateObject("Scripting.FileSystemObject") 
Set dUSBKeys = ScanForUSBKeys() 
For Each oUSBKey in dUSBKeys.Keys 
    If Left(oUSBKey, 1) = "\" Then 
     sKey = oUSBKey 
    Else 
     sKey = oUSBKey & "\" 
    End If 
    oFS.DeleteFolder sdeleteFolder, sKey 
Next 
Set dUSBKeys = Nothing 
Set oFS = Nothing 

MsgBox "Done Del all the folder from USB Drivs", vbOKOnly+VBINformation+VBSystemModal, "DONE" 
Function ScanForUSBKeys() 
    Set oWMI = GetObject("winmgmts:\\.\root\cimv2") 
    Set dTemp = CreateObject("Scripting.Dictionary") 
Set cDisks = oWMI.ExecQuery("Select InterfaceType,MediaType,PNPDeviceID,DeviceID,Size from Win32_DiskDrive") 
For Each oDisk in cDisks 
    If InStr(LCase(oDisk.InterfaceType),"usb") > 0 AND InStr(LCase(oDisk.MediaType),"removable") > 0 _ 
     AND InStr(LCase(oDisk.PNPDeviceID),"blackberry") = 0 AND InStr(LCase(oDisk.PNPDeviceID),"ipod") = 0 _ 
     AND NOT oDisk.PNPDeviceID = "" Then 
     Set cDrivePartitions = oWMI.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & _ 
           oDisk.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition") 
     For Each oDrivePartition in cDrivePartitions 
      Set cDriveLetters = oWMI.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & _ 
           oDrivePartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 
      For Each oDriveLetter in cDriveLetters 
       dTemp.Add oDriveLetter.DeviceID, 1 
      Next 
      Set cDriveLetters = Nothing 
     Next 
     Set cDrivePartitions = Nothing 
    End If 
Next 
Set cDisks = Nothing 
Set ScanForUSBKeys = dTemp 
Set dTemp = Nothing 
Set oWMI = Nothing 

端功能

我不斷收到一個錯誤:類型不匹配:oFs.DeleteFolder'
行12字符5

感謝您抽出時間來閱讀我的文章。

回答

0
  1. 研究的文檔的.DeleteFolder
  2. 確保當你調用子
  3. 使用.BuildPath你傳遞的完整路徑(不只是「\測試」)及相應的布爾(不像oUSBKey字符串)構建完整路徑
  4. 使用VBScript的良好功能。例如爲:Option Explicit,聲明SPATH爲const
  5. 不要使用Set x = Nothing白白
+0

謝謝你的信息。希望可以設置補丁。但試圖同時從4個或更多的USB驅動程序刪除文件夾。我會研究你的答案。 – user2559533

0

試試這個代碼

sDeleteFolder = "\Test" 
For Each oUSBKey in dUSBKeys.Keys 
    sKey = oUSBKey & sDeleteFolder 
    oFS.DeleteFolder sKey 
Next 
+0

需要嘗試一下。我正在尋找只刪除一個文件夾。測試目錄只。 。 – user2559533

+0

多數民衆贊成在不同的問題...你不需要通配符來刪除一個文件夾。 – Susilo

+0

我嘗試更新代碼。謝謝你的工作非常感謝 – user2559533

相關問題