【发布时间】:2013-08-22 20:34:21
【问题描述】:
我需要使用来自 vb 脚本的参数调用 perl 脚本。如果参数中包含空格,则它不起作用。请帮忙。谢谢。
Set oShell = CreateObject("WScript.Shell")
sArgs = strArg1
sExec = "perl test.pl"
sCmd = sExec & " " & sArgs & " "
oShell.Run(sCmd)
【问题讨论】:
我需要使用来自 vb 脚本的参数调用 perl 脚本。如果参数中包含空格,则它不起作用。请帮忙。谢谢。
Set oShell = CreateObject("WScript.Shell")
sArgs = strArg1
sExec = "perl test.pl"
sCmd = sExec & " " & sArgs & " "
oShell.Run(sCmd)
【问题讨论】:
您可以通过将参数括在引号中来帮助 shell 标记您的命令。
直接在 shell 上运行,可能如下所示:
C:\> perl test.pl "C:/Path with spaces/foo.temp"
至于如何在 VBScript 中执行此操作,我们可以通过两个步骤来解决:escape literal quotes in a string 和 use Replace() to format that string。
sCmd = "perl test.pl ""{0}"""
sCmd = Replace(sCmd, "{0}", sArgs)
oShell.Run(sCmd)
这假设sArgs 只包含一个参数;如果您要传递多个参数,则需要将每个参数单独括在引号中。
【讨论】:
Replace 是毫无意义的开销。一个简单的字符串连接就足够了:sCmd = "perl test.pl """ & sArgs & """"
Function qq(str) : qq = Chr(34) & str & Chr(34) : End Function),因此您可以像这样进行连接:"perl test.pl " & qq(sArgs)。