2015-10-01 24 views
1

我有一個要求,一些文件和文件夾複製到USB驅動器與特定的VolumeLabel(考慮到驅動器盤符可能會改變)複製文件和文件夾的具體卷標

我使用下面的確定,並返回,盤符;我如何使用它來複制文件到該驅動器(並覆蓋任何現有文件)

我只知道如何使用objShell.run「cmd/c複製c:\ temp \ file.xml X:\ temp文件\/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

試用一下這個例子,告訴我結果:

'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) 
相關問題