如何在批处理文件中使用 if - else 结构?

问题描述 投票:0回答:8

我有一个关于批处理文件中的 if - else 结构的问题。每个命令单独运行,但我无法安全地使用“if - else”块,因此我的程序的这些部分不起作用。我怎样才能使这些部件运行?谢谢你。

IF %F%==1 IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile%" "%destinationFile%"
    )
ELSE IF %F%==1 IF %C%==0 (
    ::moving the file c to d
    move "%sourceFile%" "%destinationFile%"
    )

ELSE IF %F%==0 IF %C%==1 (
    ::copying a directory c from d, /s:  boş olanlar hariç, /e:boş olanlar dahil
    xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
    )
ELSE IF %F%==0 IF %C%==0 (
    ::moving a directory
    xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
    rd /s /q "%sourceMoveDirectory%"
    )
batch-file if-statement
8个回答
133
投票

您的语法不正确。您不能使用

ELSE IF
。看来你其实并不需要它。只需使用多个
IF
语句即可:

IF %F%==1 IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile%" "%destinationFile%"
)

IF %F%==1 IF %C%==0 (
    ::moving the file c to d
    move "%sourceFile%" "%destinationFile%"
)

IF %F%==0 IF %C%==1 (
    ::copying a directory c from d, /s:  boş olanlar hariç, /e:boş olanlar dahil
    xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
)

IF %F%==0 IF %C%==0 (
    ::moving a directory
    xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
    rd /s /q "%sourceMoveDirectory%"
)

很棒的批处理文件参考:http://ss64.com/nt/if.html


69
投票

我认为在这个问题和一些答案中,这个伪代码在DOS中的含义有点混乱:IF A IF B X ELSE Y。它并不意味着IF(A and B)THEN X ELSE Y,而是实际上意味着 IF A( IF B THEN X ELSE Y)。如果 A 的测试失败,那么他整个内部的 if-else 将被忽略。

正如提到的答案之一,在这种情况下,只有一个测试可以成功,因此不需要“else”,但当然这只适用于这个示例,它不是执行 if-else 的通用解决方案。

有很多方法可以解决这个问题。这里有一些想法,全都很难看,但是嘿,这就是(或者至少曾经是)DOS!

@echo off

set one=1
set two=2

REM Example 1

IF %one%_%two%==1_1 (
   echo Example 1 fails
) ELSE IF %one%_%two%==1_2 (
   echo Example 1 works correctly
) ELSE (
    echo Example 1 fails
)

REM Example 2

set test1result=0
set test2result=0

if %one%==1 if %two%==1 set test1result=1
if %one%==1 if %two%==2 set test2result=1

IF %test1result%==1 (
   echo Example 2 fails
) ELSE IF %test2result%==1 (
   echo Example 2 works correctly
) ELSE (
    echo Example 2 fails
)

REM Example 3

if %one%==1 if %two%==1 (
   echo Example 3 fails
   goto :endoftests
)
if %one%==1 if %two%==2 (
   echo Example 3 works correctly
   goto :endoftests
)
echo Example 3 fails
)
:endoftests

13
投票

据我所知,你不能像在其他语言中那样批量执行

if else
,它必须嵌套
if

使用嵌套

if
你的批次看起来像

IF %F%==1 IF %C%==1(
    ::copying the file c to d
    copy "%sourceFile%" "%destinationFile%"
    ) ELSE (
        IF %F%==1 IF %C%==0(
        ::moving the file c to d
        move "%sourceFile%" "%destinationFile%"
        ) ELSE (
            IF %F%==0 IF %C%==1(
            ::copying a directory c from d, /s:  boş olanlar hariç, /e:boş olanlar dahil
            xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
            ) ELSE (
                IF %F%==0 IF %C%==0(
                ::moving a directory
                xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
                rd /s /q "%sourceMoveDirectory%"
                )
            )
        )
    )

或者正如詹姆斯建议的那样,链接你的

if
,但我认为正确的语法是

IF %F%==1 IF %C%==1(
    ::copying the file c to d
    copy "%sourceFile%" "%destinationFile%"
    )

7
投票

这是我处理 if else if 情况的方法

if %env%==dev ( 
    echo "dev env selected selected"
) else (
    if %env%==prod (
        echo "prod env selected"
    )
)

注意它与其他编程语言(如C++或Java)的if-elseif块不同,但它会做你需要做的事情


1
投票

我相信你可以使用诸如

之类的东西
if ___ (

do this

) else if ___ (

do this

)

1
投票

IF...ELSE IF
结构在批处理文件中工作得很好,特别是当您在每个 IF 行上仅使用一个条件表达式时:

IF %F%==1 (
    ::copying the file c to d
    copy "%sourceFile%1" "%destinationFile1%"
) ELSE IF %F%==0 (
    ::moving the file e to f
    move "%sourceFile2%" "%destinationFile2%" )

在您的示例中,您使用

IF...AND...IF
类型构造,其中必须同时满足 2 个条件。在这种情况下,您仍然可以使用
IF...ELSE IF
构造,但带有额外的括号以避免下一个 ELSE 条件的不确定性:

IF %F%==1 (IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile1%" "%destinationFile1%" )
) ELSE IF %F%==1 (IF %C%==0 (
    ::moving the file e to f
    move "%sourceFile2%" "%destinationFile2%"))

上述构造相当于:

IF %F%==1 (
    IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile1%" "%destinationFile1%"
    ) ELSE IF %C%==0 (
    ::moving the file e to f
    move "%sourceFile2%" "%destinationFile2%"))

批处理命令的处理顺序取决于CMD.exe解析顺序。只要确保您的构造遵循该逻辑顺序,通常它就会起作用。如果 Cmd.exe 处理您的批处理脚本没有错误,则意味着这是正确的(即您的操作系统 Cmd.exe 版本支持)构造,即使有人另有说法。


1
投票

有点晚了,也许对于复杂的 if 条件来说仍然有好处,因为我想添加一个“done”参数来保留 if-then-else 结构:

set done=0
if %F%==1 if %C%==0 (set done=1 & echo found F=1 and C=0: %F% + %C%)
if %F%==2 if %C%==0 (set done=1 & echo found F=2 and C=0: %F% + %C%)
if %F%==3 if %C%==0 (set done=1 & echo found F=3 and C=0: %F% + %C%)
if %done%==0 (echo do something)

1
投票

这是我的代码示例 if..else..if
执行以下操作

提示用户输入进程名称

如果进程名称无效
然后写入用户

Error : The Processor above doesn't seem to be exist 

如果进程名称是 services
然后写入用户

Error : You can't kill the Processor above 

如果进程名称是 valid 而不是 services
然后写入用户

进程已通过

taskill

被终止

所以我称它为进程杀手.bat
这是我的代码:

@echo off

:Start
Rem preparing the batch  
cls
Title Processor Killer
Color 0B
Echo Type Processor name to kill It (Without ".exe")
set /p ProcessorTokill=%=%  

:tasklist
tasklist|find /i "%ProcessorTokill%.exe">nul & if errorlevel 1 (
REM check if the process name is invalid 
Cls 
Title %ProcessorTokill% Not Found
Color 0A
echo %ProcessorTokill%
echo Error : The Processor above doesn't seem to be exist    

) else if %ProcessorTokill%==services (
REM check if the process name is services and doesn't kill it
Cls 
Color 0c
Title Permission denied 
echo "%ProcessorTokill%.exe"
echo Error : You can't kill the Processor above 

) else (
REM if the process name is valid and not services
Cls 
Title %ProcessorTokill% Found
Color 0e
echo %ProcessorTokill% Found
ping localhost -n 2 -w 1000>nul
echo Killing %ProcessorTokill% ...
taskkill /f /im %ProcessorTokill%.exe /t>nul
echo %ProcessorTokill% Killed...
)

pause>nul



REM If else if Template
REM if thing1 (
REM Command here 2 ! 
REM ) else if thing2 (
REM command here 2 !
REM ) else (
REM command here 3 !
REM )
© www.soinside.com 2019 - 2024. All rights reserved.