windows 如果批处理文件中的条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:59:50  来源:igfitidea点击:

If condition in batch files

windowsbatch-file

提问by bdhar

@echo off
SET var1="Yes"
SET var2="No"
SET var3="Yes"
if %var1%=="Yes"
    echo Var1 set
if %var2%=="Yes"
    echo Var2 set
if %var3%=="Yes"
    echo Var3 set

If I run the above script I get the following error. Can anyone pls help?

如果我运行上面的脚本,我会收到以下错误。任何人都可以帮忙吗?

The syntax of the command is incorrect.

命令的语法不正确。

Thanks.

谢谢。

回答by paxdiablo

The echo needs to either be at the end of the if statement:

echo 需要位于 if 语句的末尾:

if %var1%=="Yes" echo Var1 set

or of the following form:

或以下形式:

if %var1%=="Yes" (
    echo Var1 set
)

I tend to use the former for very simple conditionals and the latter for multi-command ones and primitive whilestatements:

我倾向于将前者用于非常简单的条件,而后者用于多命令和原始while语句:

:while1
    if %var1%=="Yes" (
        :: Do something that potentially changes var1
        goto :while1
    )

What your particular piece of code is doing is trying to execute the command if %var1%=="Yes"which is not valid in and of itself.

您的特定代码正在做的是尝试执行if %var1%=="Yes"本身无效的命令。

回答by Dave Webb

You can't put a newline like that in the middle of the IF. So you could do this:

你不能在IF. 所以你可以这样做:

if %var1%=="Yes" echo Var1 set

Or, if you do want your statements spread over multiple lines you can use brackets:

或者,如果您确实希望您的语句分布在多行中,您可以使用括号:

if %var1%=="Yes" (
   echo Var1 set
)

However, when you're using brackets be careful, because variable expansion might not behave as you expect. For example:

但是,当您使用方括号时要小心,因为变量扩展可能不会像您期望的那样运行。例如:

set myvar=orange

if 1==1 (
   set myvar=apple
   echo %myvar%
)

Outputs:

输出:

orange

This is because everything between the brackets is treated as a single statement and all variables are expanded before any of the command between the brackets are run. You can work around this using delayed expansion:

这是因为括号内的所有内容都被视为单个语句,并且在运行括号内的任何命令之前扩展所有变量。您可以使用延迟扩展来解决此问题:

setlocal enabledelayedexpansion
set myvar=orange

if 1==1 (
   set myvar=apple
   echo !myvar!
)

回答by Rubens Farias

Take a look on IF command help:

查看 IF 命令帮助:

C:\Users\Rubens>if /?
Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

So, you command must be in same IF line. Your script should be:

因此,您的命令必须在同一 IF 行中。你的脚本应该是:

@echo off
SET var1="Yes"
SET var2="No"
SET var3="Yes"
if %var1%=="Yes" echo Var1 set
if %var2%=="Yes" echo Var2 set
if %var3%=="Yes" echo Var3 set

回答by Nick

@echo off
setlocal enabledelayedexpansion

set var1=1
set var2=2
set var3=1

if "!var1!" == "1" echo Var1 set
if "!var2!" == "1" echo Var2 set
if "!var3!" == "1" echo Var3 set
pause