【问题标题】:Call perl script from vbscript从 vbscript 调用 perl 脚本
【发布时间】: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)

【问题讨论】:

    标签: perl vbscript arguments


    【解决方案1】:

    您可以通过将参数括在引号中来帮助 shell 标记您的命令。

    直接在 shell 上运行,可能如下所示:

    C:\> perl test.pl "C:/Path with spaces/foo.temp"
    

    至于如何在 VBScript 中执行此操作,我们可以通过两个步骤来解决:escape literal quotes in a stringuse 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)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多