4

I'm working on the following batch script and I'm not sure how I can achieve the results I'm looking for. I searched here as well as online, but I didn't see anything that looked like what I'm looking for. Any help is appreciated.

I need to check if two files exist as well as if they don't. Currently I've got the code below working, however if one file exists or is missing it goes in a 3rd direction (I've updated the code to account for the 3rd direction so now it works fine, but I know there is much room for improvement!). I know it's not pretty, but it works and the pretty code doesn't.

Please note that there are other files in the same directory with the same extensions, so searching by extension will not work.

Working Code:

@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

What I would like to do:

(The following code isn't expected to work, it's my thoughts written out)

@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

Example of why checking for the two files alone will not work

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
Gerhard
  • 22,678
  • 7
  • 27
  • 43
Mike Roh
  • 62
  • 1
  • 1
  • 10

5 Answers5

4

The parentheses are neccessary for the else clause.

@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

But the else isn't required at all because the program flow falls through to the download label

@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
  • This doesn't work as I've mentioned above. Edit: Apparently you can't post code in comments so I will post it above. – Mike Roh Apr 21 '18 at 10:20
  • Sorry but the fault has to be something else - above code runs here. –  Apr 21 '18 at 10:41
  • My apologies, you're correct. Your code above does work "as written". It was very late and I wasn't thinking clearly. After modifying the code to take into account how my script is structured I was able to get it working. – Mike Roh Apr 23 '18 at 23:52
2

Why perenthesize? Simply try.

@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"

or in a loop:

@echo off
for %%i in ("file1.exe" "file2.zip") do if not exist "%%i" echo %%i Must be downloaded & goto :EOF
echo Successful Installation
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • I have tested this already and you're not taking into account that 1 file maybe missing or present which will fail/pass the checks. (Sorry just updated the page and I can see you've changed the code.) – Mike Roh Apr 21 '18 at 10:08
  • the if exist condition checks both files, if either does not exist it does not complete. test again, I made an edit earlier because I misread the question – Gerhard Apr 21 '18 at 10:09
  • Thank you these are both good suggestions, but I have come to realize the use of "if not exist" is redundant. LotPings second lower example above was the answer I was looking for. – Mike Roh Apr 24 '18 at 00:01
0
:again
for %%a in ("file1.exe" "file2.zip") do if not exist "%%~a" call :download "%%~a" &goto again

And then write an internal procedure to download the missing file supplied as %1

Possibly you'd want to install a counter and report in the :download procedure if the download is being executed, and if so of which file - and count the number of iterations to stop the process if the download fails too often.

An advantage here is that there's no real limit to the number of filenames that can be included in the parentheses

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Found the solution! Added some if exist parameters to account for empty folders.

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..
Kirk
  • 21
  • 1
  • 2
0

Download & execute

@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
Cody MacPixelface
  • 1,348
  • 10
  • 21