【问题标题】:How can I make a Batch file with multiple else conditions?如何制作具有多个其他条件的批处理文件?
【发布时间】:2018-05-11 06:38:45
【问题描述】:

我想制作一个包含多个 else 条件的批处理文件,并且已经搜索了很多,但我的不工作。我希望该文件检查是否存在两个文件,如果存在则打开其中一个文件。如果两个文件之一不存在,则批处理应比较接下来的两个文件。我的文件是这样的:

IF EXIST "file1.txt" IF EXIST "file2.txt" Goto V1

IF EXIST "file3.txt" IF EXIST "file4.txt" Goto V2

IF EXIST "file5.txt" AND IF EXIST "file6.txt" Goto V3

:V1
cd "folder"
start sample.exe
goto commonexit

:V2
cd "folder2"
start sample2.exe
goto commonexit

:V3
cd "folder3"
start sample3.exe
goto commonexit

:commonexit

这样 cmd 打开并立即关闭。当我注释掉“:commonexit”时,它会打开并通过代码工作,但似乎在双IF条件下(IF ... IF ...)它只关心第二个IF。在它们之间放置一个 AND 运算符并没有帮助。

你们有没有人猜测,可能是什么问题?

编辑: :commonexit 正在工作。我只是不知道 :commonexit 之后的断线会使代码损坏。

【问题讨论】:

  • 在批处理中没有什么能比得上“AND 运算符”。你是用双击开始的吗?如果是,请注意,工作文件夹可能不是您所期望的。与echo on 一起运行以观察它的真正作用。 (并在:commonexit 后面加上pause 以防止窗口关闭)
  • 嗨斯蒂芬,感谢您的回复。我不知道的是,当 :commonexit 之后有断线时,代码将不起作用。我将使用 Gerhard 的代码再次尝试并发布我的结果。
  • 我建议使用例如Start /D "folder2" sample2.exe 可能比使用cd "folder2" 后跟start sample2.exe 更好。
  • 嗯...您在谈论else 子句,但我在您发布的代码中找不到单个else... ;-)

标签: batch-file


【解决方案1】:

更简单更接近原代码:

IF EXIST "file1.txt" IF EXIST "file2.txt" cd "folder" & start sample.exe & goto commonexit

IF EXIST "file3.txt" IF EXIST "file4.txt" cd "folder2" & start sample2.exe & goto commonexit

IF EXIST "file5.txt" IF EXIST "file6.txt" cd "folder3" & start sample3.exe & goto commonexit

rem Put here code that will execute when none of the previous paths execute...

:commonexit

编辑添加了新方法

下面的新方法允许对多个“EXIST 文件名”测试编写有效的“AND”操作,并以类似于多个“ELSE IF”命令的方式链接其中几个测试:

(for /F "skip=1" %%a in ('dir /B file1.txt file2.txt') do break) && (
   echo Both file1.txt and file2.txt exists
   echo Process they here
) || (for /F "skip=1" %%a in ('dir /B file3.txt file4.txt') do break) && (
   echo Both file3.txt and file4.txt exists
   echo Process they here
) || (for /F "skip=1" %%a in ('dir /B file5.txt file6.txt') do break) && (
   echo Both file5.txt and file6.txt exists
   echo Process they here
) || echo Last else

此方法基于for /F 命令返回的“ExitCode”值,如this answer 所述(在退出代码管理部分下方)。该方法可以很容易地扩展到更多文件,只需插入额外的文件名将“skip=1”值调整为文件数减一。例如:

(for /F "skip=2" %%a in ('dir /B file1.txt file2.txt file3.txt') do break) && (
   echo All file1.txt, file2.txt and file3.txt exists
   echo Process they here
) || (for /F "skip=2" %%a in ('dir /B file4.txt file5.txt file6.txt') do break) && (
   echo All file4.txt, file5.txt and file6.txt exists
   echo Process they here
) || echo Last else

【讨论】:

  • 接受邀请! 这很聪明,虽然它看起来比我自己的答案更奇怪,但我认为扩展更多文件并扩展更多命令会更容易比我自己的。它对外部流程的依赖也更少,而且应该更快!
【解决方案2】:

这是一种方法:

@echo  off
if exist "file1.txt" if exist "file2.txt" (
cd "folder"
start sample.exe
goto :EOF
)

if exist "file3.txt" if exist "file4.txt" (
cd "folder2"
start sample2.exe
goto :EOF
)

if exist "file5.txt" if exist "file6.txt" (
cd "folder3"
start sample3.exe
goto :EOF
)

逻辑如下: 检查file1 是否存在,如果存在则检查file2 是否存在,如果存在,cd,执行样本然后goto 文件结束。但是,如果file1file2 不存在,它将跳过当前代码块并转到if 的下一行

【讨论】:

  • 嗨格哈德,感谢您的回复。我按照你的方式尝试过,但即使 file1 不存在,Batch 也想打开 sample1.exe。猜猜我怎么能改变它?
  • 我使用了您的示例 1,它打开了 sample1.exe,但缺少 file1.txt
  • 我现在开始工作了。它仍然是这个缺失的 AND 连接。所以我将双 IF 条件 (IF NOT ... IF NOT ... GOTO ...) 放在两行 (IF NOT ... GOTO ... \LINEBREAK IF NOT ... IF NOT ... GOTO 中。 ..)。它似乎不是很有条理,但它有效
【解决方案3】:

这是另一种单行解决方案,(拆分为 3 以保持最大 80 个字符的行长度),它使用 Where 命令:

(Where/Q "file1.txt"&&(Where/Q "file2.txt"&&Start /D "folder" "sample.exe"))||(
    Where/Q "file3.txt"&&(Where/Q "file4.txt"&&Start /D "folder2" "sample2.exe"
))||Where/Q "file5.txt"&&Where/Q "file6.txt"&&Start /D "folder3" "sample3.exe"

【讨论】:

  • 这不起作用。第一个ELSE在“file1.txt”存在“file2.txt”不存在时执行,但如果“file1.txt”不存在,则不执行其他代码。也就是说:第一个ELSE属于第二个IF命令;第一个IF 命令没有任何ELSE 部分。同一点适用于“file3.txt”和“file5.txt”的“IF”。
  • @Aacini,你是对的,这是一个懒惰的答案,我在发布之前没有正确查看问题。我已经完全修改了答案以使用另一种方法。
  • 我邀请您查看我在my answer 发布的新方法... ;)
猜你喜欢
  • 1970-01-01
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
相关资源
最近更新 更多