If not exist

May 27, 2009 at 18:24:25
Specs: Windows XP
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.


See More: If not exist

Report •


#1
May 27, 2009 at 21:10:13
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


Report •

#2
May 28, 2009 at 11:17:08
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


Report •

#3
May 28, 2009 at 11:39:18
@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


Report •

Related Solutions

#4
May 28, 2009 at 13:21:00
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.


Report •

#5
May 28, 2009 at 13:29:26
if %ERRORLEVEL%==9009


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
May 28, 2009 at 13:34:03
Does this catch all unknown errors?

Report •

#7
May 28, 2009 at 13:39:38
Does this catch all unknown errors?
No, but IF ERRORLEVEL 1 will catch all errors.

Report •

#8
May 28, 2009 at 13:42:14
I had used if errorlevel 1 before and someone else told me to use the errorlevel 9009.

Thanks.


Report •

#9
May 28, 2009 at 13:43:35
is there a way to trap the exact error message being thrown?

Report •

#10
May 28, 2009 at 13:52:21
IF %ERRORLEVEL%==<whatever> may or may not work, depending on where you use it.

Report •

#11
May 28, 2009 at 17:41:39
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.


Report •

#12
May 28, 2009 at 18:03:00
Well, error codes are dependent on the application, so you'd also need to tell them what application threw the error.

Report •


Ask Question