How to set environment variables in vbs that can be read when calling a batch script

I have a batch file that calls a vbscript file. I am trying to modify a vbscript file of an environment variable that will later be used in a batch file that calls a vbscript file.

Here are the fragments 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" 
+7
vbscript
source share
5 answers

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

There are, of course, other ways of transferring information back to the parent batch file - output to standard output or a file is an obvious way, for example.

 == Child.vbs === WScript.echo "New Value" == Parent.cmd === for /f "tokens=*" %%i in ('cscript //nologo child.vbs') do set Value=%%i echo %Value% 
+5
source share

yes, you can ... however, you will have to restart your session. see the following link:

Is there a command to update environment variables from the command line on Windows?

'RESETVARS.vbs

 Set oShell = WScript.CreateObject("WScript.Shell") filename = oShell.ExpandEnvironmentStrings("%TEMP%\resetvars.bat") Set objFileSystem = CreateObject("Scripting.fileSystemObject") Set oFile = objFileSystem.CreateTextFile(filename, TRUE) set oEnv=oShell.Environment("System") for each sitem in oEnv oFile.WriteLine("SET " & sitem) next path = oEnv("PATH") set oEnv=oShell.Environment("User") for each sitem in oEnv oFile.WriteLine("SET " & sitem) next path = path & ";" & oEnv("PATH") oFile.WriteLine("SET PATH=" & path) oFile.Close 

Here is how I did it:

 SET oShell = CREATEOBJECT("Wscript.Shell") dim varSet SET varSet = NOTHING SET varSet = oShell.Environment("SYSTEM") varSet("WinVer") = "6.0.2008" 

Then in a separate VB script (resetvars.vbs) I called from a CMD script:

 cscript //nologo \\%APPSERVER%\apps\IE9.0\restartvars.vbs call %TEMP%\resetvars.bat 
+2
source share

I do not think you can do this. At the very least, you will need to mess around with the environment block in the calling process, and there is no guarantee that it will respect this ...

0
source share

Ho about it:

 @echo off set vbsFile=%temp%\createguid.vbs call :CreateVbs call :GetGuid NewGuid echo.%NewGuid% del %vbsFile%>nul GOTO:EOF :CreateVbs echo.set obj = CreateObject("Scriptlet.TypeLib")>%vbsFile% echo.WScript.StdOut.WriteLine obj.GUID>>%vbsFile% GOTO:EOF :GetGuid for /f "tokens=*" %%i in ('cscript //nologo %vbsFile%') do set %1=%%i GOTO:EOF 

This is not a pure batch script, but works fine.

0
source share
 @echo off&color 4a&title %~n0&AT>NUL IF %ERRORLEVEL% EQU 0 ( goto 2 ) ELSE ( echo. ) if not "%minimized%"=="" goto 1 set minimized=true & start /min cmd /C "%~dpnx0"&cls&exit :1 wmic process where name="cmd.exe" CALL setpriority "realtime">nul&echo set shell=CreateObject("Shell.Application") > %~n0.vbs&echo shell.ShellExecute "%~dpnx0",,"%CD%", "runas", 1 >> %~n0.vbs&echo set shell=nothing >> %~n0.vbs&start %~n0.vbs /realtime&timeout 1 /NOBREAK>nul& del /Q %~n0.vbs&cls&exit :2 echo %~dpnx0 admin mode look up&wmic process where name="cmd.exe" CALL setpriority "realtime"&timeout 3 /NOBREAK>nul :3 echo x=msgbox("end of line" ,48, "%~n0") > %~n0.vbs&start %~n0.vbs /realtime&timeout 1 /NOBREAK>nul& del /Q %~n0.vbs&cls&exit 
0
source share

All Articles