Re: How to set environment variable via JScript?
- From: "David Mayerovitch" <davmay@xxxxxxxxxxxx>
- Date: Tue, 26 Sep 2006 23:05:27 -0400
Thanks, Walter. I tried your batch and script; found that the cscript logo
output was messing it up, so I changed your line
for /f "tokens=*" %%a in ('cscript setvar.js') do %%a
to
for /f "tokens=*" %%a in ('cscript //nologo setvar.js') do %%a
and it worked perfectly.
After some study of the lengthy FOR documentation I was able to figure out
how that line works; the DOS batch language, I now realize, is more powerful
than I'd thought, but rather cryptic. I think I'll have an easier time with
all-scripting solutions.
Best wishes,
David
"Walter Zackery" <please_respond_to@xxxxxxxxx> wrote in message
news:e%23VybgH4GHA.1300@xxxxxxxxxxxxxxxxxxxxxxx
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");
-------------------------------------
.
- References:
- How to set environment variable via JScript?
- From: David Mayerovitch
- Re: How to set environment variable via JScript?
- From: Walter Zackery
- How to set environment variable via JScript?
- Prev by Date: Re: Javascript function required
- Next by Date: Re: Internet Explorer 6 vs. 7
- Previous by thread: Re: How to set environment variable via JScript?
- Next by thread: Drag Drop between Windows
- Index(es):
Relevant Pages
|