BATCH 文件中多个文件名的条件 IF EXIST 语句
Conditional IF EXIST Statement for multiple filenames in BATCH file
我正在处理以下批处理脚本,但我不确定如何才能获得我想要的结果。我在这里和网上都搜索过,但没有看到任何看起来像我要找的东西。任何帮助表示赞赏。
我需要检查两个文件是否存在以及它们是否不存在。目前我有下面的代码工作,但是如果一个文件存在或丢失它会进入第三个方向(我已经更新了代码以说明第三个方向所以现在它工作正常,但我知道有很多空间求改进!)。我知道它不漂亮,但它可以工作,而漂亮的代码却不行。
请注意,同一目录下还有其他具有相同扩展名的文件,因此无法按扩展名搜索。
工作代码:
@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
为什么插入?简单地尝试。
@echo off
echo checking file structure...
if exist "file1.exe" if exist "file2.zip" echo Install successful
if not exist "file1.exe" echo downloading missing "file1.exe"
if not exist "file2.zip" echo downloading missing "file2.zip"
或在一个循环中:
@echo off
for %%i in ("file1.exe" "file2.zip") do if not exist "%%i" echo %%i Must be downloaded & goto :EOF
echo Successful Installation
:again
for %%a in ("file1.exe" "file2.zip") do if not exist "%%~a" call :download "%%~a" &goto again
然后编写一个内部程序来下载丢失的文件 %1
可能您想要安装一个计数器并在 :download
过程中报告是否正在执行下载,如果是哪个文件 - 并计算迭代次数以停止该过程,如果下载经常失败。
这里的一个优点是括号中可以包含的文件名数量没有真正的限制
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
找到解决方案!添加了一些 if exist 参数来说明空文件夹。
for /D %%I in ("%~dp0*") do (
cd /D %%I
if exist *pm.wav (
if exist *.xml (
tar -cvzf %%~nxI.tar.gz *pm.wav *.xml
)
)
)
cd..
下载并执行
@echo off
echo checking file structure...
if exist "C:\Users\Himel\DesktopMB.zip" (
if exist "C:\Users\Himel\DesktopMB.zip" (
goto ok
)
)
if not exist "C:\Users\Himel\DesktopMB.zip" (
if not exist "C:\Users\Himel\DesktopMB.zip" (
goto download
)
)
if not exist "C:\Users\Himel\DesktopMB.zip" (
goto download
)
)
if not exist "C:\Users\Himel\DesktopMB.zip" (
goto download
)
)
:download
echo downloading missing files.
SETLOCAL
rem FOR DOWNLOADING FILES, THIS SCRIPT IS USING THE "BITSADMIN.EXE" SYSTEM FILE.
rem IT IS PRESENT ON MOST WINDOWS VERSION, PROBABLY FROM WINDOWS XP TO WINDOWS 10.
:SETUP
rem URL (5MB TEST FILE):
SET "FILE_URL=http://ipv4.download.thinkbroadband.com/5MB.zip"
rem SAVE IN CUSTOM LOCATION:
rem SET "SAVING_TO=C:\FolderMB.zip"
rem SAVE IN THE CURRENT DIRECTORY
SET "SAVING_TO=5MB.zip"
SET "SAVING_TO=%~dp0%SAVING_TO%"
:MAIN
ECHO.
ECHO DOWNLOAD SCRIPT EXAMPLE
ECHO.
ECHO FILE URL: "%FILE_URL%"
ECHO SAVING TO: "%SAVING_TO%"
ECHO.
rem UNCOMENT AND MODIFY THE NEXT LINE IF YOU NEED TO USE A PROXY SERVER:
rem CALL :DOWNLOAD_PROXY_ON "PROXY-SERVER.COM:8080"
rem THE MAIN DOWNLOAD COMMAND:
CALL :DOWNLOAD_FILE "%FILE_URL%" "%SAVING_TO%"
rem UNCOMMENT NEXT LINE FOR DISABLING THE PROXY (IF YOU USED IT):
rem CALL :DOWNLOAD_PROXY_OFF
:RESULT
ECHO.
IF EXIST "%SAVING_TO%" ECHO YOUR FILE HAS BEEN SUCCESSFULLY DOWNLOADED.
IF NOT EXIST "%SAVING_TO%" ECHO ERROR, YOUR FILE COULDN'T BE DOWNLOADED.
ECHO.
:EXIT_SCRIPT
PAUSE
EXIT /B
rem FUNCTIONS SECTION
:DOWNLOAD_FILE
rem BITSADMIN COMMAND FOR DOWNLOADING FILES:
bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2
GOTO :EOF
:DOWNLOAD_PROXY_ON
rem FUNCTION FOR USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob OVERRIDE %1 "<local>"
GOTO :EOF
:DOWNLOAD_PROXY_OFF
rem FUNCTION FOR STOP USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob NO_PROXY
GOTO :EOF
share
:ok
echo Install successful
我正在处理以下批处理脚本,但我不确定如何才能获得我想要的结果。我在这里和网上都搜索过,但没有看到任何看起来像我要找的东西。任何帮助表示赞赏。
我需要检查两个文件是否存在以及它们是否不存在。目前我有下面的代码工作,但是如果一个文件存在或丢失它会进入第三个方向(我已经更新了代码以说明第三个方向所以现在它工作正常,但我知道有很多空间求改进!)。我知道它不漂亮,但它可以工作,而漂亮的代码却不行。
请注意,同一目录下还有其他具有相同扩展名的文件,因此无法按扩展名搜索。
工作代码:
@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
为什么插入?简单地尝试。
@echo off
echo checking file structure...
if exist "file1.exe" if exist "file2.zip" echo Install successful
if not exist "file1.exe" echo downloading missing "file1.exe"
if not exist "file2.zip" echo downloading missing "file2.zip"
或在一个循环中:
@echo off
for %%i in ("file1.exe" "file2.zip") do if not exist "%%i" echo %%i Must be downloaded & goto :EOF
echo Successful Installation
:again
for %%a in ("file1.exe" "file2.zip") do if not exist "%%~a" call :download "%%~a" &goto again
然后编写一个内部程序来下载丢失的文件 %1
可能您想要安装一个计数器并在 :download
过程中报告是否正在执行下载,如果是哪个文件 - 并计算迭代次数以停止该过程,如果下载经常失败。
这里的一个优点是括号中可以包含的文件名数量没有真正的限制
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
找到解决方案!添加了一些 if exist 参数来说明空文件夹。
for /D %%I in ("%~dp0*") do (
cd /D %%I
if exist *pm.wav (
if exist *.xml (
tar -cvzf %%~nxI.tar.gz *pm.wav *.xml
)
)
)
cd..
下载并执行
@echo off
echo checking file structure...
if exist "C:\Users\Himel\DesktopMB.zip" (
if exist "C:\Users\Himel\DesktopMB.zip" (
goto ok
)
)
if not exist "C:\Users\Himel\DesktopMB.zip" (
if not exist "C:\Users\Himel\DesktopMB.zip" (
goto download
)
)
if not exist "C:\Users\Himel\DesktopMB.zip" (
goto download
)
)
if not exist "C:\Users\Himel\DesktopMB.zip" (
goto download
)
)
:download
echo downloading missing files.
SETLOCAL
rem FOR DOWNLOADING FILES, THIS SCRIPT IS USING THE "BITSADMIN.EXE" SYSTEM FILE.
rem IT IS PRESENT ON MOST WINDOWS VERSION, PROBABLY FROM WINDOWS XP TO WINDOWS 10.
:SETUP
rem URL (5MB TEST FILE):
SET "FILE_URL=http://ipv4.download.thinkbroadband.com/5MB.zip"
rem SAVE IN CUSTOM LOCATION:
rem SET "SAVING_TO=C:\FolderMB.zip"
rem SAVE IN THE CURRENT DIRECTORY
SET "SAVING_TO=5MB.zip"
SET "SAVING_TO=%~dp0%SAVING_TO%"
:MAIN
ECHO.
ECHO DOWNLOAD SCRIPT EXAMPLE
ECHO.
ECHO FILE URL: "%FILE_URL%"
ECHO SAVING TO: "%SAVING_TO%"
ECHO.
rem UNCOMENT AND MODIFY THE NEXT LINE IF YOU NEED TO USE A PROXY SERVER:
rem CALL :DOWNLOAD_PROXY_ON "PROXY-SERVER.COM:8080"
rem THE MAIN DOWNLOAD COMMAND:
CALL :DOWNLOAD_FILE "%FILE_URL%" "%SAVING_TO%"
rem UNCOMMENT NEXT LINE FOR DISABLING THE PROXY (IF YOU USED IT):
rem CALL :DOWNLOAD_PROXY_OFF
:RESULT
ECHO.
IF EXIST "%SAVING_TO%" ECHO YOUR FILE HAS BEEN SUCCESSFULLY DOWNLOADED.
IF NOT EXIST "%SAVING_TO%" ECHO ERROR, YOUR FILE COULDN'T BE DOWNLOADED.
ECHO.
:EXIT_SCRIPT
PAUSE
EXIT /B
rem FUNCTIONS SECTION
:DOWNLOAD_FILE
rem BITSADMIN COMMAND FOR DOWNLOADING FILES:
bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2
GOTO :EOF
:DOWNLOAD_PROXY_ON
rem FUNCTION FOR USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob OVERRIDE %1 "<local>"
GOTO :EOF
:DOWNLOAD_PROXY_OFF
rem FUNCTION FOR STOP USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob NO_PROXY
GOTO :EOF
share
:ok
echo Install successful