tags:

views:

36

answers:

2

I have a batch file that calls a vbscript file. I am trying to have the vbscript file change an environment variable that is later used in the batch file that calls the vbscript file.

Here are snippetes from the files.

Parent.bat

Set Value="Initial Value"
cscript Child.vbs
ECHO Value = %VALUE%

Child.vbs

Set wshShell = CreateObject( "WScript.Shell" )
Set wshSystemEnv = wshShell.Environment( "Process" )
wshSystemEnv("VALUE") = "New Value"
+1  A: 

You can't. A process can pass environment variables to child processes, but not to its parent - and in this case the parent is cmd.exe, which is running your Parent.bat file.

There are of course other ways to communicate information back to the parent batch file - outputting to stdout or a file is an obvious way, e.g.

== Child.vbs ===
WScript.echo "New Value"

== Parent.cmd ===
for /f "tokens=*" %%i in ('cscript //nologo child.vbs') do set Value=%%i
echo %Value%
Joe
A: 

I don't think you can do this. At least, you would need to mess with the environment block in the calling process, and there's no guarantee that it will respect this...

SamB