3

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

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

5 Comments

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)
@user My bad. There was an ampersand supposed to be between the " " and qq(arg). Fixed.
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?
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.
@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.
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).

4 Comments

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 - 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).
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.
This question is about passing the arguments. Your code is not passing them but instead creates its own arguments.
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

1 Comment

This question is about passing the arguments. Your code is not passing them but instead creates its own arguments.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.