tags:

views:

462

answers:

3

How do I in a batch script find the full path to application XYZ if it is installed

Clarifications:

  1. The application is not in the PATH
  2. All I have is it's name in this case "ISTool.exe" and I would like to get C:\Program\ISTool\ISTool.exe
+3  A: 

You can locate an executable on the path (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 can be found at the end of the help text 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 modifiers can be combined to get compound 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 path (as per your edit), your best bet is to use the bare/subdirectory format of dir which will do it for you. From the root directory:

dir /b /s ISTool.exe

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

Update:

That dir /b /s command will take a while since it's basically searching the whole disk. If that's a problem you may want to consider periodically creating a cached record of all files on all disks 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 scheduled tasks either nightly (for an always-on server) or on boot (for a desktop). You could even make the script more intelligent to do it only every couple of days (I have an automated backup script that does a similar thing on the family machines I support). This creates the list in a temporary cache file then overwrites the original one to ensure the time when the file doesn't exist is minimized.

Then you can just use:

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

to locate all your files.

paxdiablo
Ahh, but it's not in the path. I'll edit the question to make that clear.
Nifle
dir /b /s ISTool.exe works *kind of*. But it takes a prohibitive long time. It took 43 seconds on my current computer.
Nifle
Have you thought of upgrading? :-) No, seriously, it has to search the entire disk so it will take a while. Is this the sort of thing you're going to be doing a lot of? If so, you may want to consider the idea of caching that information, see my update.
paxdiablo
+1  A: 

This is the closest I got. One drawback is that it works only for one drive per execution, but that could made more flexible. Another is the output, that always contains a // between the path and the filename. But per definition 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 for loop iterates through all directories starting at the given path (C:\) and check if the filename exists in that directory. If so, both variables are concatenated and stored in %fullpath% and the loop is terminated by a jump.

Frank Bollack
A: 

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

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

The result is fast, has few lines of code but is not very 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 results in "C:\Program\ISTool\ISTool.exe" (with quotes)
Note: delims= above is followed by a tab-char

Nifle