Is it possible to set an environment variable to the output of a command in cmd.exe

StackOverflow https://stackoverflow.com/questions/3199696

  •  02-10-2019
  •  | 
  •  

문제

I need to do the equivalent of

set ENVAR=`some-command`

In a windows/cmd.exe script. Cygwin is not an option.

For bonus marks: Is there some cmd.exe equivalent of backticks in general?

도움이 되었습니까?

해결책

A quick and dirty way would be redirecting it to a file and then reading this, e.g.

some-command>out.txt
set /p ENVAR=<out.txt

I think for can also help you, but I don't remember the exact syntax. Try something like

for /f "usebackq" %x in (`some-command`) do set ENVAR=%x

I probably forgot some token or delim in the options...

다른 팁

Not "probably", it is absolutely a must to specify "delims=" as last token (means, no delimiters), unless you want your variable only contain up to first space of the input data.

I.e.

FOR /F "usebackq delims=" %%a IN (`cygpath.exe -u "%~1"`) DO (
  SET CMDNAME=%%~a
  SHIFT
)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top