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

前端 未结 6 764
礼貌的吻别
礼貌的吻别 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 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.

提交回复
热议问题