ChatGPT解决这个技术问题 Extra ChatGPT

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

我对批处理文件中的 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%"
    )
我的问题读清楚了吗?我逐行阅读,但在我看来似乎是并排的。
您好,欢迎来到 Stack Overflow!为了创建代码块,您可以突出显示相关文本并单击 {} 按钮;它有助于使帖子更具可读性并避免一些标记问题。在回答您的问题时,它现在可以!编写问题时,下面有一个预览,因此您可以看到发布后的外观。
所以谢谢你的解释。我将使用这些方法。
如果有人想把 multiple commands 放在一个 if 中也可以看到这个帖子:stackoverflow.com/questions/13692916/…

J
James Hill

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


如果我错了,请纠正我,但我认为您不能在多个 if 条件下使用 &&,看看我的替代语法吗?
如果我的 if 条件是这样的: "IF %F%==0" 安全运行,但是当我添加 && 时它没有运行。如何在 if 中使用两个条件?
@aprogrammer,看看我的例子。如果您有多个条件,请使用 IF condition1 IF condition 2
我已经尝试过您的答案,但我无法运行它,但我再次尝试并修改了它运行的代码。我的代码的某些部分的最后情况是这样的: 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 ( ::从 d 复制目录 c, /s: boş olanlar hariç, /e:boş olanlar dahil xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e )
你是什么意思你不能使用ELSE IF?在Win7下运行良好。参见示例:paste2.org/G8tMae92
w
wjandrea

我认为在问题和一些答案中,对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

我认为前两个 set 不需要百分号;应该是 set one=1
B
Bali C

AFAIK 你不能像用其他语言那样批量执行 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%"
    )

if else,尤其是在类 C 语言中,也只是嵌套的 if。您也不必将它们与批处理文件中的块一起嵌套。
@Joey 我不太关注。我知道 else if 组合只是嵌套的 if,但我认为您不能像在 C 语言中那样批量使用这些关键字。
嗯,我仍然无法让它工作,但我会相信你的话,你知道你在说什么,我会留下这个答案作为一个长版本:)
感谢您的回答,但没有一个没有在我的代码中运行。
另一个例子: if 1==1 (echo 1) else (if 1==1 (echo 2) else (echo 3))
A
Amado Saladino

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

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

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


O
Ophir Yoktan

我相信您可以使用诸如

if ___ (

do this

) else if ___ (

do this

)

第二个条件中的语句不应该是“这样做”吗?
@bvj 不,不,你完全错了。它应该是 do foodo bar
当您写 IF (test) (command) ELSE IF (test) (command) 时,您是在暗示 IF (test) (command) ELSE (IF (test) (command))。这有时可能会起作用,但如果您认为它是 DOS 中可接受的实际编程结构,那么它将是一个 PITA,以便在它失败时进行故障排除。
S
Stefane

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

C
Community

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 parsing order。只需确保您的构造遵循该逻辑顺序,并且通常它会起作用。如果您的批处理脚本由 Cmd.exe 处理而没有错误,则意味着这是正确的(即受您的操作系统 Cmd.exe 版本支持)构造,即使有人另有说明。


O
Oimar Daif

这是我的 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 )