Reputation: 16341
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?
Upvotes: 20
Views: 13079
Reputation: 924
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.
Upvotes: 1
Reputation: 2565
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
Upvotes: 1
Reputation: 26150
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.
Upvotes: 9