如何在batch file中使用if – else结构?

我有一个batch file中的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%" ) 

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

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

作为提到的答案之一,在这种情况下,只有一个testing可以成功,所以'其他'是不需要的,但当然这只适用于这个例子,这不是一个通用的解决scheme,做if-else。

有很多方法可以解决这个问题。 这里有几个想法,都是相当难看的,但嘿,这是(或至less是)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 

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

使用嵌套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%" ) 

我相信你可以使用诸如

 if ___ ( do this ) else if ___ ( do this ) 

有点晚了,也许仍然适合复杂的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) 

IF...ELSE IF在batch file中构造工作得很好,特别是在每个IF行上只使用一个条件expression式时:

 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...IFtypes构造,其中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.exeparsing顺序 。 只要确保你的构造遵循这个逻辑顺序,并且通常会起作用。 如果您的批处理脚本由Cmd.exe处理而没有错误,则表示这是正确的(即由您的操作系统Cmd.exe版本支持)结构,即使别人说了别的。