How to test if an executable exists in the %PATH% from a windows batch file?

后端 未结 9 649
暖寄归人
暖寄归人 2020-12-22 20:33

I\'m looking for a simple way to test if an executable exists in the PATH environment variable from a Windows batch file.

Usage of external tools not provided by th

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 21:13

    Windows Vista and later versions ship with a program called where.exe that searches for programs in the path. It works like this:

    D:\>where notepad
    C:\Windows\System32\notepad.exe
    C:\Windows\notepad.exe
    
    D:\>where where
    C:\Windows\System32\where.exe
    

    For use in a batch file you can use the /q switch, which just sets ERRORLEVEL and doesn't produce any output.

    where /q myapplication
    IF ERRORLEVEL 1 (
        ECHO The application is missing. Ensure it is installed and placed in your PATH.
        EXIT /B
    ) ELSE (
        ECHO Application exists. Let's go!
    )
    

    Or a simple (but less readable) shorthand version that prints the message and exits your app:

    where /q myapplication || ECHO Cound not find app. && EXIT /B
    

提交回复
热议问题