How do I find full path to an application in a batch script

前端 未结 6 785
礼貌的吻别
礼貌的吻别 2021-02-19 00:25

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 h
相关标签:
6条回答
  • 2021-02-19 00:42

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

    0 讨论(0)
  • 2021-02-19 00:51

    Based on the really helpful answers here I hacked up these two batches which I thought I share here (I know this thread is now 3 years old, but its found as 1st match 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 because there are allways two tests, but its fast enough so I didnt further bother about; sure its possible to 1st do a separate 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%
    
    0 讨论(0)
  • 2021-02-19 00:51

    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.

    0 讨论(0)
  • 2021-02-19 01:01

    Sometimes this simple solution works, where you check to see if the output matches what you expect. The first line runs the command 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.
    
    0 讨论(0)
  • 2021-02-19 01:06

    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.

    0 讨论(0)
  • 2021-02-19 01:06

    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 QUERY 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

    0 讨论(0)
提交回复
热议问题