我正在处理以下批处理脚本,而且我不确定如何才能实现我正在寻找的结果。我在这里以及在线搜索,但我没有看到任何看起来像我正在寻找的东西。任何帮助表示赞赏。
我需要检查是否存在两个文件以及它们是否存在。目前我已经得到了下面的代码,但是如果一个文件存在或者缺少它会进入第三个方向(我已经更新了代码来解释第三个方向,所以现在它工作正常,但我知道还有很大的提升空间!)我知道它并不漂亮,但它很有效,漂亮的代码也没有。
请注意,同一目录中还有其他文件具有相同的扩展名,因此按扩展程序搜索将无效。
工作代码:
@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
答案 0 :(得分:0)
为什么要进行perenthesize?试试吧。
@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
答案 1 :(得分:0)
:again
for %%a in ("file1.exe" "file2.zip") do if not exist "%%~a" call :download "%%~a" &goto again
然后编写内部过程以下载以%1
如果下载正在执行,你可能想安装一个计数器并在:download
程序中报告,如果是,那么也要报告 - 如果下载失败,则计算停止进程的迭代次数经常。
这里的一个优点是,括号中可以包含的文件名数量没有实际限制
答案 2 :(得分:0)
括号是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
但其他并不是必需的,因为程序流程会下载到下载标签
@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
答案 3 :(得分:0)
找到了解决方案!添加了一些如果存在的参数来说明空文件夹。
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..
答案 4 :(得分:0)
下载并执行
@echo off
echo checking file structure...
if exist "C:\Users\Himel\Desktop\5MB.zip" (
if exist "C:\Users\Himel\Desktop\5MB.zip" (
goto ok
)
)
if not exist "C:\Users\Himel\Desktop\5MB.zip" (
if not exist "C:\Users\Himel\Desktop\5MB.zip" (
goto download
)
)
if not exist "C:\Users\Himel\Desktop\5MB.zip" (
goto download
)
)
if not exist "C:\Users\Himel\Desktop\5MB.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:\Folder\5MB.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