Computing.Net > Forums > Programming > If not exist

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

If not exist

Reply to Message Icon

Name: needToCode
Date: May 27, 2009 at 18:24:25 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 27, 2009 at 21:10:13 Pacific
Reply:

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


0

Response Number 2
Name: needToCode
Date: May 28, 2009 at 11:17:08 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: May 28, 2009 at 11:39:18 Pacific
Reply:

@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


0

Response Number 4
Name: needToCode
Date: May 28, 2009 at 13:21:00 Pacific
Reply:

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.


0

Response Number 5
Name: Mechanix2Go
Date: May 28, 2009 at 13:29:26 Pacific
Reply:

if %ERRORLEVEL%==9009


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

M2


0

Related Posts

See More



Response Number 6
Name: needToCode
Date: May 28, 2009 at 13:34:03 Pacific
Reply:

Does this catch all unknown errors?


0

Response Number 7
Name: Razor2.3
Date: May 28, 2009 at 13:39:38 Pacific
Reply:

Does this catch all unknown errors?
No, but IF ERRORLEVEL 1 will catch all errors.


0

Response Number 8
Name: needToCode
Date: May 28, 2009 at 13:42:14 Pacific
Reply:

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

Thanks.


0

Response Number 9
Name: needToCode
Date: May 28, 2009 at 13:43:35 Pacific
Reply:

is there a way to trap the exact error message being thrown?


0

Response Number 10
Name: Razor2.3
Date: May 28, 2009 at 13:52:21 Pacific
Reply:

IF %ERRORLEVEL%==<whatever> may or may not work, depending on where you use it.


0

Response Number 11
Name: needToCode
Date: May 28, 2009 at 17:41:39 Pacific
Reply:

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.


0

Response Number 12
Name: Razor2.3
Date: May 28, 2009 at 18:03:00 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: If not exist

Batch - if [not] exist www.computing.net/answers/programming/batch-if-not-exist/11906.html

If [not] exist - ignore previous www.computing.net/answers/programming/if-not-exist-ignore-previous/11907.html

if not exist executes without permission www.computing.net/answers/programming/if-not-exist-executes-without-permission/19826.html