The Code Cave

March 22, 2006

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

Filed under: Batch — Brian @ 12:49 am

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:
  1. :: *************************************************************************
  2. ::  SearchPath.Bat                                                                                            10/31/2005
  3. ::  Written by Brian Layman (AKA Capt. Queeg)
  4. ::  Visit him at http://www.TheCodeCave.com
  5. :
  6. ::  A batch written to display the program that would be run when
  7. ::  a filename is typed at the command prompt.  Just a demo for
  8. ::  Hartmut at http://www.ztw3.com/forum/forum.cgi
  9. :
  10. ::  Usage: SearchPath ProgramName[.EXT]
  11. :
  12. ::  History:
  13. ::     10/31/2005 - BL - Created
  14. ::     11/01/2005 - BL - Removed Temp File Usage
  15. :
  16. :: *************************************************************************
  17. @echo Off
  18. :: All this is boiled down to one subroutine that sets a variable of the
  19. :: same name.
  20. call :SearchedFilePath %1
  21.  
  22. :: If no program is found, say so.
  23. if "%SearchedFilePath%"=="" echo There is no matching program in the search path
  24.  
  25. :: If a program was found, echo its name.
  26. if NOT "%SearchedFilePath%"=="" echo %SearchedFilePath%
  27.  
  28. :: Clear out our temp variable
  29. set SearchedFilePath=
  30.  
  31. :: Quit
  32. GOTO :EOF
  33. :: *************************************************************************
  34.  
  35.  
  36. :: *************************************************************************
  37. ::  Support procedures
  38. ::
  39. ::  These routines are called with a CALL directive and the GOTO :EOF
  40. ::  terminates that CALL but does not terminate the entire running of the
  41. ::  batch file.
  42. :: *************************************************************************
  43.  
  44. :: *************************************************************************
  45. :SearchedFilePath
  46. ::  Returns the full path to a passed file in the searchpath
  47. ::
  48. ::  Returns blank if not found.
  49. ::
  50. :: *************************************************************************
  51. : set SearchedFilePath
  52.   :: Set the default value to blank.
  53.   set SearchedFilePath=
  54.  
  55.   :: If there is no extension handle it
  56.   if "%~x1"=="" Call :SearchWithExtensions %1&GOTO :EOF
  57.  
  58.   :: There is no extension, is it blank?
  59.   if "%1"=="" GOTO :EOF
  60.  
  61.   :: So, we have an extension.  That means we can do a simple search.
  62.   :: %~dp$PATH:1 automatically searches the path for us.  It is a
  63.   :: variable set by the Call command.
  64.   set  SearchedFilePath=%~dp$PATH:1%1
  65.   if "%SearchedFilePath%"=="%1" set SearchedFilePath=&GOTO :EOF
  66.   GOTO :EOF
  67. :: *************************************************************************
  68.  
  69. :: *************************************************************************
  70. :SearchWithExtensions
  71. ::  Iterates the extensions gathered from the PATHEXT environment
  72. ::  and searches until the file is found.
  73. ::
  74. ::  Returns blank if not found.
  75. ::
  76. :: *************************************************************************
  77.     :: Initialize a counter for looking at multiple search results in one line
  78.     set cnt=0
  79.  
  80.     :SearchLoop
  81.       :: Break out after 20  checks.
  82.       :: If you might have more than 20 extensions, increase this value.
  83.       :: If you could find out how many periods there are in the temp file,
  84.       :: you could optimize this.
  85.       if "%cnt%"=="20" GOTO :SearchLoopCleanup
  86.       set /A cnt=%cnt%+1
  87.       :: Continually search the single line file returning each sequential
  88.       :: search result and recursively pass it to the SearchedFilePath routine. 
  89.       :: When we ask for a token # that doesn’t exist and blank is returned,
  90.       :: abort out.
  91.       for /F "tokens=%cnt% delims=.;" %%C in ("%PATHEXT%") do call :SearchedFilePath %1.%%C
  92.       if "%SearchedFilePath%"== "" GOTO :SearchLoop
  93.    
  94.     :SearchLoopCleanup
  95.       :: Clear our Temp variable
  96.       set cnt=
  97.   GOTO :EOF
  98. :: *************************************************************************

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress