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

Batch file 如何在批处理文件中使用if-else结构?,batch-file,if-statement,Batch File,If Statement,我有一个关于批处理文件中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 m

我有一个关于批处理文件中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 %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%"
    )

伟大的批处理文件参考:

如果你不能像其他语言那样批量处理
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%"
                )
            )
        )
    )
或者正如James所建议的那样,如果
,请将你的
链接起来,不过我认为正确的语法是

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

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

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

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


我相信你可以用一些东西,比如

if ___ (

do this

) else if ___ (

do this

)

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…和…IF
类型构造,其中必须同时满足两个条件。在这种情况下,您仍然可以使用
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处理且没有错误,则表示这是正确的(即,您的OS Cmd.exe版本支持)构造,即使有人不这么说。

有点晚了,可能对于复杂的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
以下哪一项

提示用户输入进程名称

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

Error : The Processor above doesn't seem to be exist 
Error : You can't kill the Processor above 
如果进程名称为服务
然后是写给用户

Error : The Processor above doesn't seem to be exist 
Error : You can't kill the Processor above 
如果进程名称有效而不是服务
然后是写给用户

Error : The Processor above doesn't seem to be exist 
Error : You can't kill the Processor above 
进程已通过
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 )

下面是我如何处理if-else-if情况

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

<强> > < >强如>与其他程序语言(如C++或java一样),但它将做你需要做的事情:“/p>我的问题是清楚地阅读吗?我一行一行地阅读,但我似乎是并排的。嗨,欢迎到堆栈溢出!为了创建代码块,你可以突出相关文本并点击

{}
按钮;它有助于使帖子更具可读性,并避免了一些加分问题。现在回答您的问题时,它就可以了!在编写问题时,下面有一个预览,这样您可以看到发布后的效果。因此,感谢您的解释。我将使用这些方法。如果有人想使用
多个命令,我将n one
if
也可以看到这篇帖子:
if else
,特别是在C类语言中,它也是一个嵌套的
if
。你也不必在批处理文件中用块来嵌套它们。@Joey我不太明白。我知道
else if
组合只是一个嵌套的
if
,但我不认为你可以用这些关键字来我仍然不能让它工作,但我相信你的话,你知道你在说什么,我将把这个答案作为一个长版本:)谢谢你的回答,但没有一个没有在我的代码中运行。另一个例子:if 1==1(echo 1)else(if 1==1(echo 2)else(echo 3))如果我错了,请纠正我,但我认为您不能在多个
if
条件中使用
&&
,请参阅我的替代语法?如果我的if条件是这样的:“if%F%==0”安全运行,但当我添加&&it时,它不会运行。我如何在if中使用两个条件?@a程序员,请看我的示例。如果您有多个条件,请使用
if condition 1 if condition 2
我尝试了您的答案,但无法运行它,但我再次尝试并修改了我的代码以使其运行。我代码的某些部分的最后一种情况如下这是:如果%F%==1如果%C%==1(::将文件C复制到d复制“%sourceFile%”“%destinationFile%”)其他(如果%F%==1如果%C%==0(::将文件C移动到d移动“%sourceFile%”“%destinationFile%”)其他(如果%F%==0如果%C%==1(::从d复制目录C,/s:bo olar hariç,/e:bo olar dahil xcopy)“%sourceCopyDirectory%”“%destinationCopyDirectory%”“/s/e”)如果
,您不能使用
否则是什么意思?它在Win7下工作正常。参见示例:我认为前两个
集合
不需要百分号;应该是
集合一=1
第二个条件中的语句不应该是“这样做吗?”“?@bvj不,不,你全搞错了。应该是
do foo
do bar
。当你写
IF(test)(command)ELSE IF(test)(command)
时,你是在暗示
IF(test)(command)ELSE(IF(test)(command))
。这有时可能会起作用,但如果您认为这是DOS中可以接受的实际编程结构,那么当它失败时,将是一个PITA来进行故障排除