将文件和文件夹复制到特定的卷标

时间:2015-10-01 14:29:50

标签: vbscript

我要求将一些文件和文件夹复制到带有特定卷标的USB驱动器(考虑到驱动器号可能会更改)

我使用以下内容来确定并返回驱动器号;我如何使用它将文件复制到该驱动器(并覆盖任何现有文件)

我只知道如何使用objShell.run" cmd / c copy c:\ temp \ file.xml X:\ temp file \ / y"但在这种情况下显然无法使用它。

 Set objShell = CreateObject("Wscript.Shell")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For Each objDrive in colDrives

  Select Case objDrive.DriveType 
    Case 1
    If objDrive.VolumeName = "MyUSB" Then ' 
      Message = Message & "Drive letter: " & objDrive.DriveLetter & VbCrLf ' 
      Message = Message & "Drive type: " & objDrive.DriveType & VbCrLf '
      Message = Message & "Volume name: " & objDrive.VolumeName & VbCrLf & VbCrLf ' 
    End If
  End Select
Next

1 个答案:

答案 0 :(得分:1)

试试这个例子并告诉我结果:

'Show drive letters associated with each
DriveLetter = ""
ComputerName = "."
Set wmiServices  = GetObject ( _
    "winmgmts:{impersonationLevel=Impersonate}!//" _
    & ComputerName)
' Get physical disk drive
Set wmiDiskDrives =  wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")

For Each wmiDiskDrive In wmiDiskDrives
   ' x = wmiDiskDrive.Caption & Vbtab & " " & wmiDiskDrive.DeviceID 
    'Use the disk drive device id to
    ' find associated partition
    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"    
    Set wmiDiskPartitions = wmiServices.ExecQuery(query)

    For Each wmiDiskPartition In wmiDiskPartitions
        'Use partition device id to find logical disk
        Set wmiLogicalDisks = wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
             & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 
x = ""
        For Each wmiLogicalDisk In wmiLogicalDisks
            'x = x & wmiDiskDrive.Caption & " " & wmiDiskPartition.DeviceID & " = " & wmiLogicalDisk.DeviceID
            'Wscript.echo x
            Wscript.echo "The DriveLetter of your USB Key is = " & wmiLogicalDisk.DeviceID & "\"
            DriveLetter = wmiLogicalDisk.DeviceID
        Next      
    Next
Next

Set WS = CreateObject("WScript.Shell")
Command = "cmd /c copy c:\temp\file.xml " & DriveLetter & "\ /y"
wscript.echo Command
Result = ws.run(Command,0,False)

或类似的东西:

Set Ws = CreateObject("WScript.Shell")
Set FSO=CreateObject("Scripting.FileSystemObject")
For each Drv in FSO.Drives
    If Drv.DriveType=0 Then Dtype="Unknown "
    If Drv.DriveType=1 Then Dtype="Removable"
    If Drv.DriveType=2 Then Dtype="Fixed "
    If Drv.DriveType=3 Then Dtype="Network "
    If Drv.DriveType=4 Then Dtype="CD-ROM "
    If Drv.DriveType=5 Then Dtype="RAM Disk "
    If Drv.IsReady Then
        If Drv.DriveType=1 Then
            Dfree=Drv.FreeSpace
            DfreeMB=FormatNumber(Drv.FreeSpace/(1024^2),0)&" MB"
            DriveLetter=Drv.DriveLetter
        End if
    End if
Next
MsgBox "Espace Libre dans Le Flash Disk " & DriveLetter & ":\"&" est Environ de " & DfreeMB,64,"Espace Libre"
Command = "cmd /c copy c:\temp\file.xml " & DriveLetter & ":\ /y"
wscript.echo Command
Result = ws.run(Command,0,True)