batch-file - BATCH 文件中多个文件名的条件 IF EXIST 语句

标签 batch-file if-statement conditional goto file-exists

我正在处理以下批处理脚本,但不确定如何实现我正在寻找的结果。我在这里和在线搜索,但我没有看到任何看起来像我正在寻找的东西。任何帮助表示赞赏。

我需要检查两个文件是否存在以及它们是否存在。目前我有下面的代码工作,但是如果一个文件存在或丢失它会在第三个方向(我已经更新了代码以说明第三个方向,所以现在它工作正常,但我知道有很大的空间改进!)。我知道它不漂亮,但它有效,而漂亮的代码却没有。

请注意,同一目录中还有其他文件具有相同的扩展名,因此按扩展名搜索将不起作用。

工作代码:

@echo off
echo checking file structure...
if exist "file1.exe" (
if exist "file2.zip" (
goto ok
)
)

if not exist "file1.exe" (
if not exist "file2.zip" (
goto download
)
)
if not exist "file1.exe" (
goto download
)
)

if not exist "file2.zip" (
goto download
)
)
:download
echo downloading missing files.
:ok
echo Install successful

我想做什么:

(以下代码预计不起作用,这是我写出来的想法)
@echo off
set file1=setup.exe
set file2=package.zip

if exist $file1 && $file2 then goto ok
else if not exist $file1 || $file2 goto download

为什么单独检查这两个文件不起作用的示例
echo checking file structure...
if exist "setup.exe" if exist "package.zip" goto TEST1
if not exist "setup.exe" if not exist "package.zip" goto TEST2
ECHO [Error!] - 1 File is present, the other is missing!
PAUSE
:TEST1
ECHO [Success!] - Found both files, YAY! 
PAUSE
:TEST2
ECHO [Error!] - No matching files found.
PAUSE

最佳答案

else 需要括号条款。

@echo off
echo checking file structure...
if exist "file1.exe" (
    if exist "file2.zip" (
        goto :ok
    ) else goto :download
) else goto :download

:download
echo downloading missing files.
:ok
echo Install successful

但是 else 根本不需要,因为程序流落入下载标签
@echo off
echo checking file structure...
if exist "file1.exe" if exist "file2.zip"  goto :ok
:download
echo downloading missing files.
:ok
echo Install successful

关于batch-file - BATCH 文件中多个文件名的条件 IF EXIST 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49954239/

相关文章:

if-statement - 批处理脚本输入问题

android - 批量重命名 - Android Bootanimation - 将文件重命名为 1 到 100 之间的不同步骤

c++ - 什么更快 : noop function call or if statement?

javascript - 尝试了解如何将 If 语句与事件一起使用

neo4j - 是否可以根据密码查询中的某些条件创建变量并进行分配?

windows - 批处理文件暂停需要两次按键才能关闭

r - 当通过 `r BATCH script file` 调用时,如何在 R 中获取当前执行脚本的名称

string - 测试字符串是否为有效整数

Javascript 开关与 if...else if...else

python - 根据 if-elif-else 条件创建新列