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

余生长醉 提交于 2019-12-05 03:41:24

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
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!