user505210
user505210

Reputation: 1402

calling vbscript from another vbscript file passing arguments

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

Upvotes: 2

Views: 27842

Answers (3)

mountainclimber11
mountainclimber11

Reputation: 1400

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

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

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

Upvotes: 5

Andy G
Andy G

Reputation: 19367

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).

Upvotes: 0

Related Questions