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

前端 未结 6 766
礼貌的吻别
礼貌的吻别 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: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 ^
      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 ^ ^
      exit /b
    )
    setlocal
    for /f "delims=" %%A in ('dir /b /s %1\%2') do set F=%%A
    if exist "%F%" echo %F%
    

提交回复
热议问题