A batch file to find the first matching file in the search path…

This is another small app I wrote for someone to use at the ZTree forum.

This one is a DOS batch file that finds files in your search path that match the criteria you pass to it.
For instance you could type “SearchPath WPFile.Doc” and it would return the location of that file in your search path. If you type in simply “SearchPath MyApp” it will do a search for all executible files as defined by the PATHEXT environmental variable.

I found this program extremely useful since when clicking Start>Run and entering NO (it was supposed to autocomplete to notepad) ran a program that I could not find anywhere. SearchPath found it in a network directory.

Concepts Demonstrated:
Batch file subroutines – Use a CALL to execute a jump location in the same batch file as if it was a seperate batch
GOTO :EOF – Used to return out of a batch file OR batch Subroutine.
For Loops & Accessing to Environment variables
Child Recursion – A parent calls the child and the child in turn calls the parent which could in turn
child again.

[DOS]
:: *************************************************************************
:: SearchPath.Bat 10/31/2005
:: Written by Brian Layman (AKA Capt. Queeg)
:: Visit him at http://www.TheCodeCave.com
::
:: A batch written to display the program that would be run when
:: a filename is typed at the command prompt. Just a demo for
:: Hartmut at http://www.ztw3.com/forum/forum.cgi
::
:: Usage: SearchPath ProgramName[.EXT]
::
:: History:
:: 10/31/2005 – BL – Created
:: 11/01/2005 – BL – Removed Temp File Usage
::
:: *************************************************************************
@echo Off
:: All this is boiled down to one subroutine that sets a variable of the
:: same name.
call :SearchedFilePath %1

:: If no program is found, say so.
if “%SearchedFilePath%”==”” echo There is no matching program in the search path

:: If a program was found, echo its name.
if NOT “%SearchedFilePath%”==”” echo %SearchedFilePath%

:: Clear out our temp variable
set SearchedFilePath=

:: Quit
GOTO :EOF
:: *************************************************************************

:: *************************************************************************
:: Support procedures
::
:: These routines are called with a CALL directive and the GOTO :EOF
:: terminates that CALL but does not terminate the entire running of the
:: batch file.
:: *************************************************************************

:: *************************************************************************
:SearchedFilePath
:: Returns the full path to a passed file in the searchpath
::
:: Returns blank if not found.
::
:: *************************************************************************
: set SearchedFilePath=
:: Set the default value to blank.
set SearchedFilePath=

:: If there is no extension handle it
if “%~x1″==”” Call :SearchWithExtensions %1&GOTO :EOF

:: There is no extension, is it blank?
if “%1″==”” GOTO :EOF

:: So, we have an extension. That means we can do a simple search.
:: %~dp$PATH:1 automatically searches the path for us. It is a
:: variable set by the Call command.
set SearchedFilePath=%~dp$PATH:1%1
if “%SearchedFilePath%”==”%1” set SearchedFilePath=&GOTO :EOF
GOTO :EOF
:: *************************************************************************

:: *************************************************************************
:SearchWithExtensions
:: Iterates the extensions gathered from the PATHEXT environment
:: and searches until the file is found.
::
:: Returns blank if not found.
::
:: *************************************************************************
:: Initialize a counter for looking at multiple search results in one line
set cnt=0

:SearchLoop
:: Break out after 20 checks.
:: If you might have more than 20 extensions, increase this value.
:: If you could find out how many periods there are in the temp file,
:: you could optimize this.
if “%cnt%”==”20” GOTO :SearchLoopCleanup
set /A cnt=%cnt%+1
:: Continually search the single line file returning each sequential
:: search result and recursively pass it to the SearchedFilePath routine.
:: When we ask for a token # that doesn’t exist and blank is returned,
:: abort out.
for /F “tokens=%cnt% delims=.;” %%C in (“%PATHEXT%”) do call :SearchedFilePath %1.%%C
if “%SearchedFilePath%”== “” GOTO :SearchLoop

:SearchLoopCleanup
:: Clear our Temp variable
set cnt=
GOTO :EOF
:: *************************************************************************[/DOS]

Add a Comment

Your email address will not be published. Required fields are marked *