2

I am using the below script to call another script .The issue is I have to pass the arguments which I retrieve by WScript.Arguments to the second script that I am calling .can someone please tell me how to do that.

Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")

objShell.Run "TestScript.vbs"    

Set objShell = Nothing
user505210
  • 1,362
  • 11
  • 28
  • 50

3 Answers3

5

You need to build your argument list with proper quoting of the arguments. You also need to differentiate between named and unnamed arguments. At the very minimum, all arguments with spaces in them must be put between double quotes. It doesn't hurt, though, to simply quote all arguments, so you could do something like this:

Function qq(str)
  qq = Chr(34) & str & Chr(34)
End Function

arglist = ""
With WScript.Arguments
  For Each arg In .Named
    arglist = arglist & " /" & arg & ":" & qq(.Named(arg))
  Next
  For Each arg In .Unnamed
    arglist = arglist & " " & qq(arg)
  Next
End With

CreateObject("WScript.Shell").Run "TestScript.vbs " & Trim(arglist), 0, True
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • I got this error on the `Expected end of statement` on the line `arglist = arglist & " " qq(arg)`, right before the first character on `qq(arg)` – Evandro Coan Jul 21 '17 at 10:23
  • @user My bad. There was an ampersand supposed to be between the `" "` and `qq(arg)`. Fixed. – Ansgar Wiechers Jul 23 '17 at 21:26
  • The "it doesn't hurt" part is wrong. At least the `find` command expects the quotes in the command line. Is there a way to preserve them? – Roland Illig Nov 25 '18 at 07:47
  • Could you explain why distinguishing between named and unnamed arguments is required? Just collecting each `WScript.Arguments(i)` in a loop recreates the original command line as well, and it is simpler code. – Roland Illig Nov 25 '18 at 07:52
  • @RolandIllig Last time I checked `find` was not a VBScript, but an external command. If you actually read the question (and my answer) you may notice that neither of them is about running arbitrary external commands. – Ansgar Wiechers Nov 25 '18 at 17:19
0

Use:

objShell.Run "TestScript.vbs arg1 arg2"

If one of the arguments contains spaces then you will need to embed these in quotes, probably like this:

objShell.Run "TestScript.vbs arg1 arg2 ""this is three"""

or it may accept apostrophes (I haven't tried this recently).

Andy G
  • 19,232
  • 5
  • 47
  • 69
  • In my First script I have this statement Set objArgs = WScript.Arguments and I want to pass this objArgs as argument to the next script..It is just passing objArgs as a string to the next script not as object with values. – user505210 Jul 03 '13 at 00:10
  • @user505210 - you can't pass objects as arguments to processes created with .Run or .Exec; you'll have to send all the desired info as/converted to strings (not to hard as WScript.Arguments(.Named) is a list of strings). – Ekkehard.Horner Jul 03 '13 at 00:21
  • The arguments you pass when using `.Run` are essentially just strings, you can't pass an object with this simple command-line method - event though the receiving script stores them in an object:Arguments. If you really wanted to pass an object between scripts you would have to serialize and de-serialize them from a separate file - which is not a simple process with VBScript. I suggest, with Ekkehard, that you just pass strings/values to the second script. – Andy G Jul 03 '13 at 00:24
  • This question is about _passing_ the arguments. Your code is not passing them but instead creates its own arguments. – Roland Illig Nov 25 '18 at 07:49
0

I found the answers a little confusing, so here is mine, which in my mind shows it more simply. The other answer aren't wrong, just different (slightly).

in test.vbs file:

Set shell = CreateObject("WScript.Shell")
shell.CurrentDirectory = "C:\some\path\"
x = "testing"
shell.Run "test1.vbs " & x 

in C:\some\path\test1.vbs file:

x = WScript.Arguments.Item(0) 
msgbox x

resulting message box from test.vbs file, passed to test1.vbs file:

testing
mountainclimber11
  • 1,339
  • 1
  • 28
  • 51
  • This question is about _passing_ the arguments. Your code is not passing them but instead creates its own arguments. – Roland Illig Nov 25 '18 at 07:49