Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Variables from one .VBS to another

Status
Not open for further replies.

SWarrior

MIS
Dec 19, 2003
111
US
I'm sure this has been asked before, but I can't find it in any of the threads. I need to pass multiple variables from one .VBS to another .VBS. I've accomplished getting the script to call another script without proceeding, but I need to pass variables back and forth. Sometimes from different starting locations.

Sometimes
vbs1 -> vbs2

Sometimes
vbs2 -> vbs1

& Sometimes
vbs1 -> vbs2 (then value change) -> vbs1

A brief example would be greatly appreciated.
Many thanks in advance!

-SWarrior
 
To call a script with arguments:
Code:
Set Sh=WScript.CreateObject("WScript.Shell")
myscript="\path\to\script.vbs"
arg1="Arg1"
arg2="Arg2"
Sh.Run myscript & " " & arg1 & " " & arg2
If any of the variables have embedded space you have to put them inside a pair of double quotes like this:
Code:
myscript=Chr(34) & "\path\to\my script.vbs" & Chr(34)

To retrieve the command line arguments:
Code:
Set args=WScript.Arguments
For i=0 To args.Count-1
  WScript.Echo "Arg" & Cstr(i+1) & "=" & args(i)
Next

Hope This Help
PH.
 
Information can easily be passed forward from one script to another, the reverse is not true, unless the information is numberic in nature, and then only one mumberic value can be returned, this value is also limited in size, I don't recall exactly what that limit is, but basically it is limited by the maximum size for a valid error number.

To send information forward, simply call the script and pass parameters to it. The second script can access the information by process it's arguments. i.e.

Argcount=Wscript.arguments.count - 1
For i = 0 To Argcount
If Wscript.arguments.item(i)="/?" Then
Goto Help
End If
Next

The only way that's built in to Windows Scripting Host to return information to the script that called the second script is to close the called script and return a single number. i.e.

'Closing code for Second Script
wscript.quit 12345

Logic can be built into the calling script to process the return code and do different things depending on the number recieved. THe code in the calling script would look something like this.

'Code from first script
ReturnCode = Shell.Run (SecondScript, 1, True)
If err.number <>0 Then
Msg= &quot;Unable to run &quot; &Program &vbCr &&quot;The following error was returned:&quot; &vbCr & err.description
Shell.popup msg,10,&quot;Attention!&quot;,80
End If
If ReturnCode=123456 Then RunSubroutineof Choice
'etc...

Here are a few more ways to perform what you are trying to do:

Use an obscurely named registry entry or entries to store the values:

Write and read the information to a temp file.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top