[ACCEPTED]-How do I find full path to an application in a batch script-batch-file

Accepted answer
Score: 32

You can locate an executable on the path 30 (or other path-like string if necessary):

c:\> for %i in (cmd.exe) do @echo. %~$PATH:i
C:\WINDOWS\system32\cmd.exe

c:\> for %i in (python.exe) do @echo. %~$PATH:i
C:\Python25\python.exe

Details 29 can be found at the end of the help text 28 for the "for" command, "for /?" but the summary is:

%~i    - expands %i removing any surrounding quotes.
%~fi   - expands %i to a fully qualified path name.
%~di   - expands %i to a drive letter only.
%~pi   - expands %i to a path only.
%~ni   - expands %i to a file name only.
%~xi   - expands %i to a file extension only.
%~si   - expanded path contains short names only.
%~ai   - expands %i to file attributes of file.
%~ti   - expands %i to date/time of file.
%~zi   - expands %i to size of file.
%~$P:i - searches the directories listed in the P environment variable
         and expands %i to the fully qualified name of the first one found.
         If the environment variable name is not defined or the file is not
         found by the search, then this modifier expands to the empty string.

The 27 modifiers can be combined to get compound 26 results:

%~dpi    - expands %i to a drive letter and path only.
%~nxi    - expands %i to a file name and extension only.
%~fsi    - expands %i to a full path name with short names only.
%~dp$P:i - searches the directories listed in the P environment variable
           for %i and expands to the drive letter and path of the first
           one found.
%~ftzai  - expands %i to a DIR like output line.

If your executable isn't on the 25 path (as per your edit), your best bet is 24 to use the bare/subdirectory format of dir which 23 will do it for you. From the root directory:

dir /b /s ISTool.exe

will 22 get you all of the files on that drive with 21 that name. You then just have to parse the 20 output. My own preference would be to use 19 Cygwin's "find /cygdrive -name ISTool.exe" but that's because I already have 18 it installed. You may not want that (or 17 even have that option).

Update:

That dir /b /s command will 16 take a while since it's basically searching 15 the whole disk. If that's a problem you 14 may want to consider periodically creating 13 a cached record of all files on all disks 12 with a cmd file like:

@echo off
setlocal enableextensions enabledelayedexpansion
del c:\files.cache.tmp >nul: 2>nul:
for %%d in (c d e) do (
    cd /d %%d:\
    dir /b /s >>c:\files.cache.tmp
)
del c:\files.cache >nul: 2>nul:
move c:\files.cache.tmp c:\files.cache
endlocal

You could do this with 11 scheduled tasks either nightly (for an always-on 10 server) or on boot (for a desktop). You 9 could even make the script more intelligent 8 to do it only every couple of days (I have 7 an automated backup script that does a similar 6 thing on the family machines I support). This 5 creates the list in a temporary cache file 4 then overwrites the original one to ensure 3 the time when the file doesn't exist is 2 minimized.

Then you can just use:

findstr \\ISTool.exe c:\files.cache

to locate 1 all your files.

Score: 9

Based on the really helpful answers here 8 I hacked up these two batches which I thought 7 I share here (I know this thread is now 6 3 years old, but its found as 1st match 5 when googling ...):

1) which.bat:

@echo off
REM emulate the Linux which command
if "%1" == "" (
  echo Usage: %~nx0 ^<command[.ext]^>
  exit /b
)
setlocal
for %%P in (%PATHEXT%) do (
  for %%I in (%1 %1%%P) do (
    if exist "%%~$PATH:I" (
      echo %%~$PATH:I
      exit /b
    )
  )
)

not perfect 4 because there are allways two tests, but 3 its fast enough so I didnt further bother 2 about; sure its possible to 1st do a separate 1 test with %1 only ...

2) findfile.bat:

@echo off
REM emulate the Linux find command
if "%1" == "" (
  echo Usage: %~nx0 ^<startdir^> ^<file^>
  exit /b
)
setlocal
for /f "delims=" %%A in ('dir /b /s %1\%2') do set F=%%A
if exist "%F%" echo %F%
Score: 3

This is the closest I got. One drawback 11 is that it works only for one drive per 10 execution, but that could made more flexible. Another 9 is the output, that always contains a // between 8 the path and the filename. But per definition 7 thats a valid path.

@ECHO OFF

SET filename=autoexec.bat

FOR /R C:\ %%a IN (\) DO (
   IF EXIST "%%a\%filename%" (

      SET fullpath=%%a%filename%
      GOTO break
   )
)
:break

ECHO %fullpath%

Will deliver: C:\\autoexec.bat

EDIT:

For explanation, the 6 for loop iterates through all directories 5 starting at the given path (C:\) and check 4 if the filename exists in that directory. If 3 so, both variables are concatenated and 2 stored in %fullpath% and the loop is terminated 1 by a jump.

Score: 0

The answers I got from others worked (but 10 slow or used extra files) and worked for 9 any exe but didn't really suit my needs.

Since 8 I wanted to find a particular exe I went 7 looking in the registry using REG QUERY instead. I 6 found a key that contained the data I wanted 5 to find and extracted that.

The result is 4 fast, has few lines of code but is not very 3 pretty nor reusable.

Short example:

@ECHO off
SETLOCAL
set found=
FOR /F "tokens=1-3 delims= " %%a IN ('REG QUERY "HKEY_CLASSES_ROOT\Applications\ISTool.exe\shell\OpenWithISTool\command"') DO (
 set found=%%c
)

for /f "tokens=1-2" %%a in ("%found%") do (
 set my_exe=%%a
)
echo %my_exe%
ENDLOCAL

This 2 results in "C:\Program\ISTool\ISTool.exe" (with quotes)
Note: delims= above 1 is followed by a tab-char

Score: 0

Sometimes this simple solution works, where 3 you check to see if the output matches what 2 you expect. The first line runs the command 1 and grabs the last line of standard output.

FOR /F "tokens=*" %%i in (' "xcopy /? 2> nul" ') do SET xcopyoutput=%%i
if "%xcopyoutput%"=="" echo xcopy not in path.
Score: 0

Alternately, programs like Everything, and UltraSearch (freeware), SwiftSearch can 8 search the MFT (of your NTFS partition) for 7 files (so it can do so very quickly), (but Wikipedia 6 claims this kind of thing can breach your 5 security model by finding things it's not 4 supposed to) -- some of them look like they 3 have some command line parameters, I've 2 not used them, but maybe it could be helpful, if 1 you're resorting to a full drive search.

More Related questions