2013-04-03 15 views
0

我正在嘗試創建一個將文件複製到另一個系統的小程序。系統的名稱先前被選中(是一個變量=「測試系統」)。我有這個Sub和Function:HTA(VBScript) - 使用變量的Shell.Run

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天。我想我錯過了一些報價,但無法弄清楚。

謝謝!

回答

2

你不會錯過引號,而是你必須對其中的很多引號。在執行之前,你的函數在命令周圍增加了額外的兩個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") 
+0

謝謝!出於某種原因,我認爲objShell.Run語句中的命令必須始終在引號中。甚至沒有跨過我的想法去除它們。 –