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

前端 未结 6 789
礼貌的吻别
礼貌的吻别 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题