Assign output of a program to a variable using a MS batch file

前端 未结 10 945
梦谈多话
梦谈多话 2020-11-22 10:06

I need to assign the output of a program to a variable using a MS batch file.

So in GNU Bash shell I would use VAR=$(application arg0 arg1). I need a si

10条回答
  •  独厮守ぢ
    2020-11-22 10:55

    In addition to the answer, you can't directly use output redirection operators in the set part of for loop (e.g. if you wanna hide stderror output from a user and provide a nicer error message). Instead, you have to escape them with a caret character (^):

    for /f %%O in ('some-erroring-command 2^> nul') do (echo %%O)
    

    Reference: Redirect output of command in for loop of batch script

提交回复
热议问题