在調用 CMD 時,如腳本中用 WScript.Shell 調用。
如果參數中有包含空格的長路徑名時,必須要加引號才能正確被識別。
是的,大家都知道要加引號,但怎麼加卻容易被誤解。這個問題,不時地會遇上,上次弄清楚了,但隔一段時間,還是忘了,同樣的問題又要重新摸索,非常痛苦。
如:
Set objShell = WScript.CreateObject("WScript.Shell")
cmd = "cmd.exe /C ""C:\Program Files\putty\pscp.exe"" -pw mypassword ""c:\Documents and Settings\myuser\fax.tif"" [email protected]:/pydio/fax/"
returnValue = objShell.Run(cmd, 0, true)
這是錯誤的,以爲只要將文件名用引號包起來就可以。
這裏執行時會提示命令找不到。
正確的方式是,將 /C 後面所有字符,再加上雙引號:
Set objShell = WScript.CreateObject("WScript.Shell")
cmd = "cmd.exe /C """"C:\Program Files\putty\pscp.exe"" -pw mypassword ""c:\Documents and Settings\myuser\fax.tif"" [email protected]:/pydio/fax/"""
returnValue = objShell.Run(cmd, 0, true)
因爲,這裏相當於有兩條命令,並且是嵌套的,外面的命令是:
cmd.exe /C "command"
命令是 cmd.exe,兩個參數分別是 /C, "command",我們這裏的 command 是
"C:\Program Files\putty\pscp.exe" -pw mypassword "c:\Documents and Settings\myuser\fax.tif" [email protected]:/pydio/fax/
如果不加外面的引號,那以空格分隔的各項,會被解析爲外面 cmd.exe 這條命令的參數,所以要出錯。
瞭解了很簡單,但沒想到這一點,一定會讓你抓狂的,而且遺憾的是,找不到相關的明確說明,大都只是簡單的說,加引號!