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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Variable to 2nd script, process and return 1

Status
Not open for further replies.

SWarrior

MIS
Dec 19, 2003
111
0
0
US
I've searched through MANY threads, and can't find a solution to my question. I am trying to pass a variable from 'Script A' to 'Script B' Script B will process that variable, and then I need 'Script B' to return that processed variable BACK to 'Script A'.

Scenerio:
Script A get's input from user: monday
Script A then passes 'monday' to Script B.
Script B processes 'monday' and makes the new
variable 'Monday'.
Script B then ends, and passes the NEW variable back
to Script A, and then Script A continues.

Thanks in Advance for any Help!
-SWarrior
 
If ScriptB needs to be completely stand alone, then what you are asking for could be tough to do.

However, if all that ScriptB contains is a function which proceesses whatever is passed to it and then returns the apropriate value then that is definatley doable.

ScriptB.VBS would contain something similar to:
Code:
OPTION EXPLICIT

FUNCTION DoStuff (strValue)
    SELECT CASE strValue
        CASE "monday"
            DoStuff = "Monday"
        CASE ELSE
            DoStuff = "More Stuff"
    END SELECT
END FUNCTION

And ScriptA.WSF would contain:
Code:
<job id="Main">
<script language="VBScript" src="ScriptB.VBS" />
<script language="VBScript">
    OPTION EXPLICIT

    WScript.Echo "Returned Value = " & DoStuff ("monday")

</script>
</Job>

This may not be the most graceful solution, but it does work.
 
[1] script_a initially has:
param_a = empty
param_b = 50 'intrinsic to itself
[2] script_b initially has:
param_a = 100 'intrinsic to itself
param_b = empty
[3] script_a like to acquire param_a from script_b with a dependency on param_b intrinsic to script_a itself.

The construction by executing script_a to end up with the desired param_a.
Code:
'script_a.vbs

script_b="d:\test\script_b.vbs"

param_a=100		'intrinsic to script_a
param_b=WScript.Arguments.Named.Item("b")
if param_b="" then
	wscript.echo "param_b is empty."
	createobject("wscript.shell").run "wscript.exe " & script_b & " /a:" & param_a,0,true
	wscript.quit
else
	wscript.echo "param_b is now " & param_b
end if
'continue to do things if so desired.
Code:
'script_b.vbs

script_a="d:\test\script_a.vbs"

param_a=WScript.Arguments.Named.Item("a")
param_b=50	'intrinsic to script_b

if param_a<>"" then
	param_b=param_b+param_a
	createobject("wscript.shell").run "wscript.exe " & script_a & " /b:" & param_b,1,true
end if
It looks limited, but if the parameter control the script block to be executed, the freedom is much larger.

Otherwise, I have to say to communicate between scripts via intermediary of a file is eventually simpler to construct.

- tsuji
 
Further notes:

My description on roles of param_a and param_b is switched over by final draft up. So in the description take what said on param_a as param_b and what on param_b as param_a. Also script_a and script_b as well. Figure it out oneself if one desires to see what's all about!

- tsuji
 
yeah, tsuji is right.
from my experience you can only pass 'back' an integer to the calling script. i.e. using the Wscript.Quit intA method.
so, unless all the info you require can be passed back using an integer you will need to communicate, as tsuji suggested, by using a file, or a register key, or something else dumped on a machine.
i think the reason for no string being passed back is something to so with ANSI or char formats?
 
If "A" is going to start a new instance of "B" just start "B" using the .Exec() method and capture the stdio streams.
 
good point dilettante, mainly cause i hadnt thought of it ;-) ha ha.

i think we should be more specific about this and actually call 'variable' 'string' in this case?

now,ive read the orginal post again, if the SWarrior really wants seperate files then a wsf and an include file is the best way to go i think. dilettante has written a useful FAQ on this subject.

von moyla
 
Ok....

I can see that this is getting WAY too confusing for me. Would it be simpler to just write my output in script A to a file, PAUSE Script A until Script B Processes that file, Writes a NEW file, and then script A Continues when Script B has returned? How could I do this then? I know how to read/write files, but how do I pause script A until script B has completed processing?

-SWarrior
 
WshShell.Run (scriptB, 1, False)

the False means scriptA should wait for process/thread scriptB to finish before it continues.

Have you read the FAQ about include files?
 
Here is another solution that is better than writing to a work file, but not as good as using a .wsf file...

Code:
'Script A

Dim objShell
Dim objEnv

Set objShell = WScript.CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("User")
objEnv("TestVar") = "10"
objShell.Run "C:\called.vbs"
MsgBox objShell.ExpandEnvironmentStrings("%TestVar%")
objEnv.Remove "TestVar"
Set objEnv = Nothing
Set objShell = Nothing

Code:
'Script B
Dim objShell
Dim objEnv

Set objShell = WScript.CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("User")
objEnv("TestVar") = CStr(CInt(objShell.ExpandEnvironmentStrings("%TestVar%")) + 5)

Set objEnv = Nothing
Set objShell = Nothing
 
Programmer1974,

Would it be safe to assume that the output of those two should be 15 ?? If so, I've tried everything that I can think of, and exactly as you have it here, the output is:
10
Asside from that, I think I'm on the right track. The variable is actually a string. I've been able to manipulate the script as you have it here, to be able to pass my variable TO Script B, however, I have been unable to get the new string Returned. I was wondering if I need to do anything tricky to assign this "Variable" to a new variable within script B, process that to another Variable, and then assign that BACK somehow to the Environment variable so that when Script B ends, Script A will then be able to pick up again??

Seriously Dazed and Confused right about now...
-SWarrior

 
HOLY BrainFart BATMAN!!!

I got it figured out now!! My Problem was that I was trying to pass the environment variable through a function. Can't do that. Had to assign the environment variable to a variable in Script B, and then run the function, and then re-assign that variable BACK to the environment!!

Works like a charm now!
Also FYI, I had to modify this line of code so that Script A waited until Script B was finished.
Code:
objShell.Run "C:\called.vbs",1,True
Someone else mentioned that 'False' waits, that was incorrect. 'True' waits for the other script to return.

Many Many THANKS to those who posted their input, trust me, i tried them!! Cudo's to Programmer1974 for his VALUABLE assistance on my issue!!

-SWarrior

The Following are excerpts of what I did to get this working. OH, BTW, this whole coding thing is for FTP file uploading ultimately. Basically to assist in writing the answer file.
Code:
' Script A  ---Variable County is assigned with the
' assistance of wshLtWtForm.ucLWF  ;)
Dim objShell
Dim objEnv
Set objShell = WScript.CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("User")
objEnv("TestVar") = County
msgbox("Script A objEnv Before Script B is: " & objEnv("TestVar"))
objShell.Run ".\Modules\ProperCase.vbs",1,True
CountyPC = objEnv("TestVar")
objEnv.Remove "TestVar"
Set objEnv = Nothing
Set objShell = Nothing
Code:
' Script B
Dim objShell
Dim objEnv
Set objShell = WScript.CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("User")
objEnv("TestVar") = CStr(CInt(objShell.ExpandEnvironmentStrings("%TestVar%")))
ProperCase = objEnv("TestVar")
Call PCase(ProperCase)
objEnv("TestVar") = ProperCase
Set objEnv = Nothing
Set objShell = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top