How to set a variable for the current OS session o

2020-03-26 04:09发布

setx permanently modifies environment variables.
set only makes variables available during batch script duration.
Is there any way to set a variable in order to hold its value until the system is restarted?
E.g. in my batch file, I'm checking if the variable is set like this:

if %MYVAR% == 1 (
<block>
)

3条回答
神经病院院长
2楼-- · 2020-03-26 04:15
@ECHO Off
IF NOT EXIST q25244121.org GOTO done
:: reset to original
FOR /f "tokens=1*delims==" %%a IN (q25244121.org) DO (
 IF DEFINED %%a FOR /f "tokens=1*delims==" %%p IN ('set %%a') DO (
  IF "%%a"=="%%p" IF "%%b" neq "%%q" SET "%%a=%%b"
 )
)
:: Delete if not originally defined
FOR /f "tokens=1*delims==" %%p IN ('set') DO (
 FINDSTR /L /i /c:"%%p=" q25244121.org >NUL
 IF ERRORLEVEL 1 SET "%%p="
)

:done
:: Record current settings
set>q25244121.org

EXIT /b

This may work for you. You would need to change the SET instructions hown in CAPS to SETX. The tempfile would no doubt also need to be placed in a file where the username is part of the name you'd use.

If you were to include this batch in your startup directory, then it should restore the last-saved environment variables' values.

So, on first logon, the current variables' values are stored. On subsequent logons, the environment would be restored to those last stored, regardless of whether a setx had been executed.

You would however need to change procedures. This will restore to a known state. If you actually wanted to setx a value or install some software which adds new environment-variable values or changes existing ones (PATH would be favourite here) then you'd need to run this routine first, make the changes, delete the save file and re-run this routine. Awkward, I'll admit - but it's a way to do it.

Oh - and remember to set your variable after having setx it. You could even write a setxX batch to setx then set (or vice-versa) the required variable.

查看更多
Deceive 欺骗
3楼-- · 2020-03-26 04:23

Ok I found a much simpler (and obviously less hacky) way than the JScript method, but it put us in the right direction with the volatile environment manipulation.

Simply use the reg command to manipulate the volatile environment in the registry :

reg add "HKCU\Volatile Environment" /v "%MYVAR%" /d "%MYVALUE%" /f

reg add both can create and update the value.

\f bypasses the overwrite confirmation.

Setting an empty data with \d "" is equivalent to deleting the global variable. You won't see it in the shell with set, but you can still see it being empty in the registry.

Here is a reference: https://ss64.com/nt/reg.html

查看更多
Deceive 欺骗
4楼-- · 2020-03-26 04:42

You may do that via a Batch-JScript hybrid script that use JScript's WshShell.Environment method. The documentation specify that there are four types of environments: System, User, Volatile and Process, and that Volatile type "Applies to current logon session and is not saved between logoffs and restarts", that is exactly what you want:

@if (@CodeSection == @Batch) @then

@echo off

rem Define a persistent variable for the current OS session only via JScript
Cscript //nologo //E:JScript "%~F0" MYVAR "This is the value"
goto :EOF

@end

var colEnvVars = WScript.CreateObject("WScript.Shell").Environment("Volatile");
colEnvVars(WScript.Arguments(0)) = WScript.Arguments(1);

You must save previous code in a .bat file and execute it as any Batch file. Note that the variable defined this way is not available in the current cmd.exe window, but only in future cmd.exe windows opened in the same OS session.

Tested on Windows 8.1.

查看更多
登录 后发表回答