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

后端 未结 9 573
暖寄归人
暖寄归人 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

    Sometimes this simple solution works, where you check to see if the output matches what you expect. The first line runs the command and grabs the last line of standard output.

    FOR /F "tokens=*" %%i in (' "xcopy /? 2> nul" ') do SET xcopyoutput=%%i
    if "%xcopyoutput%"=="" echo xcopy not in path.
    
    0 讨论(0)
  • 2020-12-22 21:16

    Use command : powershell Test-Path "exe which you looking for"

    It will return True if its present, otherwise False.

    0 讨论(0)
  • 2020-12-22 21:23

    For those looking for a PowerShell option. You can use the Get-Command cmdlet passing two items. First give the current dir location with .\ prefixed, then give just the exe name.

    (Get-Command ".\notepad", "notepad" -ErrorAction Ignore -CommandType Application) -ne $null
    

    That will return true if found local or in system wide paths.

    0 讨论(0)
提交回复
热议问题