154

我对批处理文件中的 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%"
    )
4

8 回答 8

124

你的语法不正确。你不能使用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

于 2012-06-18T11:29:10.977 回答
60

我认为在问题和一些答案中,对DOS中此伪代码的含义有些混淆:IF A IF BX ELSE Y。这并不意味着IF(A和B)THEN X ELSE Y,而是在fact 表示 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
于 2013-04-26T20:47:31.020 回答
13

AFAIK 你不能if else像用其他语言那样批量处理,它必须是嵌套if的。

使用 nestedif你的批次看起来像

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%"
    )
于 2012-06-18T11:29:56.990 回答
6

这是我如何处理 if else if 情况

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

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

于 2020-05-20T12:27:21.083 回答
2

我相信您可以使用诸如

if ___ (

do this

) else if ___ (

do this

)
于 2014-09-29T09:21:26.593 回答
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)
于 2016-10-02T11:21:52.537 回答
0

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 版本支持)构造,即使有人另有说明。

于 2016-08-08T02:36:33.587 回答
0

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

提示用户输入进程名称

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

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

如果进程名称是服务
然后它写给用户

Error : You can't kill the Processor above 

如果进程名称是有效的而不是服务
然后它写给用户

进程已通过taskill

所以我称它为Process killer.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 )
于 2018-02-28T08:26:41.380 回答