15

I need to store a value (a time stamp) and retrieve it later in batch files. So I have searched SO for answers on how to store a persistent variable and found setx.

I used it like this:

C:\tmp>setx TIME_VAR %time%

SUCCESS: Specified value was saved.

But when I try to print it with echo it is not there:

C:\tmp>echo TIME_VAR
TIME_VAR

C:\tmp>echo %TIME_VAR%
%TIME_VAR%

How do I retrieve my stored value?

code_fodder
  • 15,263
  • 17
  • 90
  • 167

3 Answers3

8

from the doc (setx /? )

Because SETX writes variables to the master environment in the registry, edits will only take effect when a new command window is opened - they do not affect the current CMD or PowerShell session.

Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • ah.... yes, that did work :), when I close my CMD and open a new one it works - which is all I really wanted, thanks very much. – code_fodder Jun 05 '14 at 14:00
  • 1
    In particular, if you use `set` *and* `setx` you've covered all situations. Note that `setx` may hang occasionally, because of other processes failing to respond properly to broadcast messages. If this is a problem, you may want to consider storing the data in a file instead. – Harry Johnston Jun 06 '14 at 03:30
1

After using setx, you don't need to wait to reboot or get in a new instance/session to be able to get/use this value. this value can be read in Windows Register:

In your case:

setx TIME_VAR %time%

for /f "tokens=3 delims=^ " %%i in ('reg query HKCU\Environment ^| findstr /i /c:"TIME_VAR"') do  echo/%%i
setx TIME_VAR %time%

for /f "tokens=3 delims=^ " %%i in ('reg query HKCU\Environment ^| findstr /i /c:"TIME_VAR"') do  (

echo/ local and next session  = = = =  
echo/ Setx Reg Value = "%%i" 

echo/ local and next session  = = = =  
echo/ TIME_VAR  Value = %%i

:eof

result:

 :: local and next session  = = = = 
 Setx Reg Value = 13:32:05,15

 :: in same session and also in next
 TIME_VAR Value = 13:32:05,15
Io-oI
  • 2,514
  • 3
  • 22
  • 29
0

I used the graphical UI "Edit the system environment variables" in the "Environment Variables..." section to view and delete the entries I set using setx. This way I was also able to verify that the setx is actually updating the variables.

Have a look at these two threads:

I was not able to view the values on the cmd nor powershell using this command either:

echo %GOPROXY%
%GOPROXY%

In my case the echo binary points to:

which echo
/usr/bin/echo

And it is not the same command as is described with

man echo

My guess is that this came with the git installation.

For me the proposed setx /? did not work either:

setx GOPROXY ""
ERROR: Invalid syntax.
Type "SETX /?" for usage.
qknight
  • 866
  • 10
  • 23
  • You're confusing Windows and Linux shells: In a Linux shell, file paths start with `/`and variables with `$`. But they are set without `$`. Example: `a=2` then `echo $a`. In the classic Windows shell CMD.exe, file paths start with a drive letter, often `C:`, and variables are surrounded by `%`. They are set using the `set` command. Example: `set a=2` then `echo %a%`. Then there is also Powershell where variables are set with preceding `$`sign: `$a=2` and it has many commands not present in a Linux shell. Windows10 + includes`wsl` as Linux shell, enable via "Turn Windows features on or off". – cachius Dec 07 '22 at 11:41
  • Some screenshots of this window can be found [here](/a/72341522). – cachius Dec 07 '22 at 11:47
  • @cachius I start cmd.exe and powershell.exe from windows+r, why would that open a linux shell? i'm a windows user with some linux tools installed into the system. true i have wsl2 installed, but i did NOT type: wsl where 'uname' reports Linux DESKTOP-18CS814 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux. instead my shells report: MSYS_NT-10.0-19044 and MSYS_NT-10.0-19044 DESKTOP-18CS814 3.3.5-341.x86_64 2022-07-08 09:41 UTC x86_64 Msys which i assume to be windows shells. – qknight Dec 09 '22 at 13:27