【问题标题】:Delete folder on multiple usb drives VBS删除多个 U 盘 VBS 上的文件夹
【发布时间】:2018-05-16 00:17:08
【问题描述】:

我希望同时删除多个 USB 驱动程序上的文件夹。

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
End Function

我不断收到错误:

 Type mismatch: 'oFs.DeleteFolder'  
 Line 12   Char 5

【问题讨论】:

    标签: vbscript usb-drive


    【解决方案1】:
    1. 研究.DeleteFolder 的文档
    2. 确保在调用 Sub 时传递完整路径(不仅仅是“\test”)和适当的布尔值(不是像 oUSBKey 这样的字符串)
    3. 使用.BuildPath 构建完整路径
    4. 使用 VBScript 的优良特性。例如:Option Explicit,将 sPath 声明为 Const
    5. 不要白白使用Set x = Nothing

    【讨论】:

    • 感谢您的信息。 Wish可以设置补丁。但试图同时从 4 个或更多 USB 驱动程序中删除该文件夹。我会调查你的答案。
    【解决方案2】:

    试试这个代码

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

    【讨论】:

    • 需要尝试一下。我希望只删除一个文件夹。仅测试目录。 .
    • 那是不同的问题...您不需要通配符来删除文件夹..
    • 我试试更新代码。谢谢,效果很好,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多