Re: How to set environment variable via JScript?

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



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");
-------------------------------------








.



Relevant Pages

  • Re: Returning a value from a VBScript
    ... VBScript for use by another script, batch file, or other command. ... echo MyScript.vbs returned an ErrorLevel of %ErrorLevel% ...
    (microsoft.public.scripting.vbscript)
  • Re: How to STOP file - a command line & script - where do you put it to work?
    ... That batch is for a very specific problem. ... that in and tell it which folders to change the names in? ... I'm a PC user, not a software, java or script> writer. ... > This is the script that was given: > @echo off ...
    (microsoft.public.windowsxp.general)
  • Re: How to STOP file - what is a command line & script?
    ... That batch is for a very specific problem. ... I'm a PC user, not a software, java or script writer. ... > @echo off ... > type nul> %temp%.\process.bat ...
    (microsoft.public.windowsxp.general)
  • Re: How do I change default permissions for shares?
    ... Doing a batch would mean ... Explorer so the only thing you need to is type in the share name the script ... If your share name is always the folder name or the folder name with ... @echo off ...
    (microsoft.public.windowsxp.security_admin)
  • Re: batch script question
    ... > I recently encountered a question on batch command, ... > the script result which I expect is that the 1st and 2nd echo output would ...
    (microsoft.public.win2000.general)