Windows command prompt: Using a variable set in the same line of a one-liner

后端 未结 4 1247
别那么骄傲
别那么骄傲 2021-02-13 23:29

Okay, I learned that one can use & or && to combine multiple commands into a single line, but it seems that variables that are set

4条回答
  •  星月不相逢
    2021-02-13 23:49

    I could not get an IF command to work with either call or cmd /v /c so I would like to offer another option for those who may need it: use FOR /F to declare and use a variable within a single command. For example:

    FOR /F %i IN ('echo 123') DO (IF %i==123 echo done)
    

    The %i is the variable that is set with the result of the IN command, which in this example is 'echo 123'.

    For values with single quotes or spaces, use the "usebackq tokens=*" flag to put the command in backquotes:

    FOR /F "usebackq tokens=*" %i IN (`echo "hello, ' ' world!"`) DO (IF %i=="hello, ' ' world!" echo done)
    

提交回复
热议问题