how do I retrieve a value after using setx?

前端 未结 2 437
梦毁少年i
梦毁少年i 2020-12-04 01:31

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.

相关标签:
2条回答
  • 2020-12-04 01:58

    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
    
    0 讨论(0)
  • 2020-12-04 02:23

    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.

    0 讨论(0)
提交回复
热议问题