【发布时间】:2014-12-01 22:57:18
【问题描述】:
我正在尝试开发一个将文件复制到特定驱动器的简单脚本。
脚本执行以下操作:
- 检查带有字母的物理驱动器
- 如果驱动器存在复制文件。
- 如果没有驱动器,请转到第 2 步。
- 检查带有字母的网络驱动器
- 如果驱动器存在复制文件。
- 如果没有驱动器,请转到第 3 步。
- 创建新的网络映射
- 将文件复制到驱动器
当存在网络驱动器或通过脚本创建网络映射驱动器时,XCopy 命令可以完美运行。 问题在于步骤 1 并且本地驱动器已经存在,调用 XCopy 后没有文件复制到驱动器。
这是我的代码:
strLocalDrive = "E:"
strRemoteShare = "\\127.0.0.1\c$\Program Files (x86)\MyFolder\EDrive"
bolFoundExisting = False
source = "C:\Program Files (x86)\MyFolder\EDrive\*"
destination = "E:\"
' Check parameters passed make sense
If Right(strLocalDrive, 1) <> ":" OR Left(strRemoteShare, 2) <> "\\" Then
'wscript.echo "Usage: cscript MapDrive.vbs drive fileshare //NoLogo"
WScript.Quit(1)
End If
'wscript.echo " - Mapping: " + strLocalDrive + " to " + strRemoteShare
'Set objNetwork = WScript.CreateObject("WScript.Network")
Set objNetwork = CreateObject("WScript.Network")
Set oShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Loop through the network drive connections and disconnect any that match strLocalDrive
Set objDrives = objNetwork.EnumNetworkDrives
' first check that physical drive does not exist
If objFSO.DriveExists(strLocalDrive) Then
WScript.echo "Physical Drive Found"
bolFoundExisting = True
ElseIf objDrives.Count > 0 Then
For i = 0 To objDrives.Count-1 Step 2
If objDrives.Item(i) = strLocalDrive Then
strShareConnected = objDrives.Item(i+1)
'objNetwork.RemoveNetworkDrive strLocalDrive, True, True
i=objDrives.Count-1
bolFoundExisting = True
End If
Next
End If
If bolFoundExisting <> True Then
WScript.echo "Drive DOES NOT exists"
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
objReg.GetStringValue HKCU, "Network\" & Left(strLocalDrive, 1), "RemotePath", strShareConnected
If strShareConnected <> "" Then
Set objReg = Nothing
bolFoundRemembered = True
End If
'Now actually do the drive map (not persistent)
Err.Clear
On Error Resume Next
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False
Else
' Drive exists copy files
WScript.echo "Drive exists"
oShell.Run "xcopy.exe " & source & " " & destination & " /C /D /E /H /I /K /R /S /Y"
Set oShell = Nothing
End IF
如果有人能解释为什么 XCOPY 命令只将文件复制到网络驱动器而不是本地驱动器,我将不胜感激!蒂亚!
更新 我已经意识到问题是由路径名中的空格引起的。 奇怪的是复制到网络驱动器确实有效,但不能复制到物理驱动器。 如何处理路径名中的空格?
【问题讨论】:
-
您在
oShell.Run语句中运行的确切命令是什么?它可以在命令提示符下工作吗? -
用引号括起来的路径?
-
@eurotrash 我不能将它们放在引号中,因为我在这样的变量中定义它们:source = "C:\Program Files (x86)\MyFolder\EDrive*" 然后像这样使用它: oShell.Run "xcopy.exe " & source & " " & destination & " /C /D /E /H /I /K /R /S /Y"
-
@Ronnyvdb 试试
oShell.Run "xcopy.exe """ & source & """ " & destination & " /C /D /E /H /I /K /R /S /Y" -
插入
chr(34)您希望引号所在的位置也可以。chr(34) & "quotedstring" & chr(34)。或者在你的情况下oShell.Run "xcopy.exe " & chr(34) & source & chr(34) & " " & chr(34) & destination & chr(34) & " /C /D /E /H /I /K /R /S /Y"。 34 是双引号的 ANSI 代码
标签: vbscript xcopy filesystemobject