Windows Batch Variables Won't Set

前端 未结 2 943
广开言路
广开言路 2020-11-22 08:04

I think I ran into a bug in Window\'s batch scripting.

I cannot set variables that are in an expanded if statement.

Here is an isolated part of my script:

2条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 08:28

    Just a remind, the ms-dos "set" command takes every string after the equal sign. So if you write

    if "x"=="x" set a=foo else set a=bar
    echo %a%  // output : foo else set a=bar
    

    The %a% is actually set to "foo else set a=bar", not foo. So I always use "()" for set command if there are multiple commands in one line.

    if "%1"=="" (set a=20) else (set a=%1)    
    

提交回复
热议问题