Last active
September 8, 2022 09:48
-
-
Save josepsmartinez/496ba062f1c2182f5993fcf395a11e57 to your computer and use it in GitHub Desktop.
Windows .bat: attributing command output to variable (solution from https://stackoverflow.com/a/48404829/4449273)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FOR /F "tokens=* USEBACKQ" %%g IN (`<command>`) do (SET <variable name>=%%g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
%%g
or%g
?As noted in the solution's thread, the
%%
sequence only works when the code line runs in a batch file. Otherwise, a single%
should be used.Example
Consider the scenario in which I want to attribute the current date (which is returned by
cmd
'sdate /t
command) to a variable calledcurrent_date
.The line to be inserted in a
.bat
script is the following:In a regular
cmd
terminal, you can debug the variable attribution (but don't forget to replace the%%
with%
!). A code block suggested for debugging the above example is the following: