Re: How to set environment variable via JScript?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Use StdOut to send values back to the batch file and change environmental
variables. For example:

::caller.bat
@echo off
SET MYVAR=Original value of MYVAR
echo Value of MYVAR is: %MYVAR%
echo Running script ...
for /f "tokens=*" %%a in ('cscript setvar.js') do %%a
echo Value of MYVAR is: %MYVAR%
pause

//setvar.js
WScript.StdOut.WriteLine("Set MYVAR=New value of MYVAR")

"David Mayerovitch" <davmay@xxxxxxxxxxxx> wrote in message
news:OkxEbFt3GHA.1288@xxxxxxxxxxxxxxxxxxxxxxx
I am running Windows XP Home with SP2.

From a batch file ("caller.bat") I would like call a JScript file
("setvar.js") to set the value of a DOS environment variable (MYVAR) which
the batch file could then manipulate. (I need to do some tweaking of
strings that the DOS batch language is incapable of doing.)

But I find with the following test files that the script fails to change
MYVAR from its original value. This failure occurs whether I use "USER",
"SYSTEM", "VOLATILE" or "PROCESS" as the parameter to the Environment
property of the WScript.Shell object.

Can anyone tell me why this setup is not working and how I can fix it?

Thanks.
David

-------------------------------------
Text of batch file "caller.bat":
-------------------------------------
@echo off
SET MYVAR=Original value of MYVAR
echo Value of MYVAR is: %MYVAR%
echo Running script ...
cscript setvar.js
echo Value of MYVAR is: %MYVAR%
pause

-------------------------------------
Text of script file "setvar.js":
-------------------------------------
function setEnvVariable(mystring){
WScript.Echo("This is setvar.js");
var sh=WScript.CreateObject("WScript.Shell");
var userEnv=sh.Environment("USER");
userEnv("MYVAR")=mystring;
return;
}

setEnvVariable("New value of MYVAR");
-------------------------------------






.



Relevant Pages

  • How to set environment variable via JScript?
    ... I am running Windows XP Home with SP2. ... the batch file could then manipulate. ... MYVAR from its original value. ... echo Value of MYVAR is: ...
    (microsoft.public.scripting.jscript)
  • Re: How to set environment variable via JScript?
    ... I tried your batch and script; ... SET MYVAR=Original value of MYVAR ... echo Value of MYVAR is: ...
    (microsoft.public.scripting.jscript)
  • using the return code of a statement directly
    ... Syntax error: word unexpected ") ... screen, but $myvar doesn't get the expected value. ... $ echo "xxx$yyy" ... echo "not zero" ...
    (comp.unix.shell)
  • Re: using the return code of a statement directly
    ... Under FreeBSD using the sh shell, what is the proper way (or rather, is ... Syntax error: word unexpected ") ... screen, but $myvar doesn't get the expected value. ... $ echo "xxx$yyy" ...
    (comp.unix.shell)
  • Re: Newbie needs answer
    ... echo $myvar; ... Files with PHP code in it will look weird or don't display at all when you display them locally or upload them to an host that doesn't have PHP support. ...
    (alt.php)