Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I want to check for the existence of several files prior to running subroutines within my batch file.
I can check for each file individually by calling something like:
:: Sets the path for the clean binaries
SET BINARYPATH=binaries\if not exist %BINARYPATH%test.exe (
echo myErrorMessage
) else (
GOTO:NextCheck
)Checking for 10 files this way is a lot of unneeded code. Is there a method to perform these checks in a few lines of scripting code?
TIA for any response.

for /f "tokens=* delims= " %%a in (filelist) do (
if not exist %BINARYPATH%%%a (
echo myErrorMessage
goto :eof
)
)
=====================================
If at first you don't succeed, you're about average.M2

Thanks.
I have tested the code and it works fine.
I have two additional questions concerning the code below.
First -- I need to stop the batch file from processing when an error has been encountered. I have tried using exit /B, but the batch file continues the processing. How do I terminate the processing?
Second -- I need to pass the variable for the missing file to the label Error. I have tried using %%a or %BINARYPATH%%%a, but that is incorrect. What is the proper way to pass this error?
*************************
SET BINARYPATH=binaries\for /f "tokens=* delims= " %%a in (%BINARYPATH%arp.exe %BINARYPATH%fileDoesNotExist.exe %BINARYPATH%route.exe) do (
if not exist %BINARYPATH%%%a (
CALL :ERROR The critical executable, "%%a", could not be located in the directory %BINARYPATH%
GOTO:EOF
)
)
:ERROR
CLS
echo.
echo.
echo ERROR MESSAGE: %*
echo.
echo.
ENDLOCAL
GOTO:EOF

@echo off
SET B=binaries\
for %%a in (arp.exe fileDoesNotExist.exe route.exe) do (
if not exist %B%%%a (
set f=%%a
goto :ERROR
)
)goto :eof
:ERROR
echo %f% not found
=====================================
If at first you don't succeed, you're about average.M2

Thanks again. Your code fixed several problems within my batch file. It also helped me reduce the code a little, by removing all the "if not exist" spread throughout the script.
The only things that I have left now are looking into handling unknown errors using "if ERRORLEVEL == 9009" and making sure any temp files (created by calls to net.exe, etc.) are written to the script directory only.
Thanks again.

if %ERRORLEVEL%==9009
=====================================
If at first you don't succeed, you're about average.M2

I had used if errorlevel 1 before and someone else told me to use the errorlevel 9009.
Thanks.

Thanks.
I think that I am just going use something like:
IF %ERRORLEVEL% NEQ 0 (
echo The following error code %ERRORLEVEL% was thrown.
)The script end user will just have to lookup the error code that is thrown.

Well, error codes are dependent on the application, so you'd also need to tell them what application threw the error.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |