我正在尝试创建一个将文件复制到另一个系统的小程序。先前已选择系统名称(是变量=“testsystem”)。我有这个Sub和功能:
Sub tst
Dim copythis
copythis = "xcopy.exe /Y c:\temp\version.bat " & testsystem & "\temp"
Set objShell = CreateObject("Wscript.Shell")
Msgbox AddQuotes(copythis)
objShell.Run AddQuotes(copythis)
End Sub
Function AddQuotes(strInput)
AddQuotes = Chr(34) & strInput & Chr(34)
End Function
messageBox显示我需要的字符串:完整命令。此外,如果我手动执行命令,它可以工作:
C:\temp>xcopy /Y c:\temp\version.bat \\testsystem3\temp
C:\temp\version.bat
1 File(s) copied
我现在已经和它斗争了2天了。我想我在某处遗漏了一些引用,但无法弄明白。
谢谢!
答案 0 :(得分:3)
你不会错过报价,而是你必须要做很多。您的函数在执行命令之前会在命令周围添加额外的双qoutes。这会导致整个命令字符串被解释为单个文件名(其中包含空格)。当您在cmd
中运行命令时,您将获得完全相同的结果:
C:\temp>"xcopy /Y c:\temp\version.bat \\testsystem3\temp"
The filename, directory name, or volume label syntax is incorrect.
只需更改此行
即可objShell.Run AddQuotes(copythis)
进入这个
objShell.Run copythis
问题应该消失。
如果您想添加双qoutes,请按以下方式添加:
copythis = "xcopy.exe /Y " & AddQuotes("c:\temp\version.bat") & " " _
& AddQuotes(testsystem & "\temp")