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

后端 未结 9 621
暖寄归人
暖寄归人 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 20:59

    This can be accomplished via parameter substitution.

    %~$PATH:1
    

    This returns the full path of the executable filename in %1, else an empty string.

    This does not work with user-defined variables. So if the executable filename is not a parameter to your script, then you need a subroutine. For example:

    call :s_which app.exe
    if not "%_path%" == "" (
      "%_path%"
    )
    
    goto :eof
    
    :s_which
      setlocal
      endlocal & set _path=%~$PATH:1
      goto :eof
    

    See http://ss64.com/nt/syntax-args.html

提交回复
热议问题