-1

I need to invoke a perl script with arguments from a vb script. if argument contains a space in it, it is not working. Pls help.Thanks.

Set oShell = CreateObject("WScript.Shell")
sArgs = strArg1
sExec = "perl test.pl"
sCmd = sExec & " " & sArgs & " "
oShell.Run(sCmd)
user2705120
  • 249
  • 5
  • 12

1 Answers1

0

You can help the shell to tokenize your command by enclosing arguments in quotes.

Run directly on a shell, that might look like this:

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

As to how you'd do that in VBScript, we can solve that with two steps: escape literal quotes in a string, and use Replace() to format that string.

sCmd = "perl test.pl ""{0}"""
sCmd = Replace(sCmd, "{0}", sArgs)
oShell.Run(sCmd)

This assumes that sArgs only contains one argument; if you're passing multiple arguments, you'll want to wrap each of them in quotes individually.

Community
  • 1
  • 1
rutter
  • 11,242
  • 1
  • 30
  • 46
  • Using `Replace` here is pointless overhead. A simple string concatenation would suffice: `sCmd = "perl test.pl """ & sArgs & """"` – Ansgar Wiechers Aug 22 '13 at 21:00
  • I expect the difference in performance is negligible, but in my opinion the difference in readability and ease of maintenance is not. – rutter Aug 22 '13 at 21:05
  • If you're concerned about readability use a quoting function (`Function qq(str) : qq = Chr(34) & str & Chr(34) : End Function`), so you can do the concatenation like this: `"perl test.pl " & qq(sArgs)`. – Ansgar Wiechers Aug 22 '13 at 21:11