VBScript XCopy到本地磁盘不工作
我试图开发一个简单的脚本,将文件复制到特定的驱动器 该脚本执行以下操作:VBScript XCopy到本地磁盘不工作,vbscript,xcopy,filesystemobject,Vbscript,Xcopy,Filesystemobject,我试图开发一个简单的脚本,将文件复制到特定的驱动器 该脚本执行以下操作: 检查带有字母的物理驱动器 如果驱动器存在,请复制文件 如果没有驱动器,请转至步骤2 用字母检查网络驱动器 如果驱动器存在,请复制文件 如果没有驱动器,请转至步骤3 创建新的网络映射 将文件复制到驱动器 当网络驱动器存在时,或者通过脚本创建网络映射驱动器时,XCopy命令可以正常工作。 问题在于步骤1和本地驱动器已经存在,调用XCopy后没有文件复制到驱动器 这是我的密码: strLocalDrive = "E:
- 如果驱动器存在,请复制文件
- 如果没有驱动器,请转至步骤2
- 如果驱动器存在,请复制文件
- 如果没有驱动器,请转至步骤3
- 将文件复制到驱动器
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
strLocalDrive=“E:”
strRemoteShare=“\\127.0.0.1\c$\Program Files(x86)\MyFolder\EDrive”
bolfundexisting=False
source=“C:\Program Files(x86)\MyFolder\edDrive\*”
destination=“E:\”
'检查传递的参数是否有意义
如果右(strLocalDrive,1)“:”或左(strRemoteShare,2)“\\”则
“wscript.echo”用法:cscript MapDrive.vbs驱动器文件共享//NoLogo
WScript.Quit(1)
如果结束
“wscript.echo”-将“+strLocalDrive+”映射到“+strRemoteShare”
'Set objNetwork=WScript.CreateObject(“WScript.Network”)
设置objNetwork=CreateObject(“WScript.Network”)
设置oShell=CreateObject(“WScript.Shell”)
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
'通过网络驱动器连接循环并断开与strLocalDrive匹配的任何连接
设置objDrives=objNetwork.enumNetworkDrive
'首先检查物理驱动器是否不存在
如果存在objFSO.drive(strLocalDrive),则
WScript.echo“找到物理驱动器”
bolfundexisting=True
ElseIf objDrives.Count>0则
对于i=0到objDrives.Count-1步骤2
如果objDrives.Item(i)=strLocalDrive,则
strShareConnect=objDrives.Item(i+1)
'objNetwork.RemoveNetworkDrive strLocalDrive,True,True
i=objDrives.Count-1
bolfundexisting=True
如果结束
下一个
如果结束
如果是真的,那么
WScript.echo“驱动器不存在”
设置objReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\\。\root\default:StdRegProv”)
objReg.GetStringValue HKCU,“网络\”和左侧(strLocalDrive,1),“远程路径”,strShareConnect
如果strShareConnect为“”,则
Set objReg=Nothing
bolfund=True
如果结束
'现在实际执行驱动器映射(不是持久的)
呃,明白了
出错时继续下一步
objNetwork.MapNetworkDrive strLocalDrive,strRemoteShare,False
其他的
'驱动器存在复制文件
WScript.echo“驱动器存在”
运行“xcopy.exe”&source&&destination&&C/D/E/H/I/K/R/S/Y
设置oShell=无
如果结束
如果有人能解释为什么XCOPY命令只将文件复制到网络驱动器而不复制到本地驱动器,我将不胜感激!蒂亚
更新
我意识到问题是由路径名中的空格引起的。
奇怪的是,复制到网络驱动器确实有效,但不能复制到物理驱动器。
如何处理路径名中的空格?您的路径中有空格,这些空格在运行
xcopy
命令时无法很好地扩展。与您需要在命令行上使用引号的方式大致相同,您需要为.Run
引用字符串。我喜欢用Chr(34)
插入双引号。34是双引号的ANSI代码
一个简单的例子是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"
为了便于阅读,您可以始终使用下划线来分隔行
oShell.Run "xcopy.exe " & chr(34) & source & chr(34) & " " & _
chr(34) & destination & chr(34) & _
" /C /D /E /H /I /K /R /S /Y"
您在
oShell.Run语句中运行的确切命令是什么?它在命令提示符下工作吗?将带有空格的路径用引号括起来?@eurotrash我不能用引号括起来,因为我在变量中定义它们:source=“C:\Program Files(x86)\MyFolder\edDrive*”,然后像这样使用它:oShell.Run“xcopy.exe”&source&=“destination&”/C/D/E/H/I/K/R/S/Y“@ronvdb TryoShell.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)&chr(34)&chr(34)&destination&chr(34)&”/C/D/E/H/I/K/R/S/Y“
.34是双引号的ANSI代码