检查 PATH 中是否存在程序

Check a program existence in PATH

我是 运行 批处理脚本(通常来自 Visual Studio)并且想检查某些程序是否可以通过 PATH 获得,如果没有 - 则显示一条消息。像

这样的简单文件检查
if exist mingw32-make (echo "exists") else (echo "not exists")

不工作 - shell 总是认为应用程序不存在(可能是因为它没有调查 PATH)。
如何正确和干净地做到这一点?

where 检查给定文件是否存在于路径中(或当前工作文件夹 %cd%)并给出完整路径或错误消息。两者都不需要 - 只需错误级别:

where mingw32-make >nul 2>&1
if errorlevel 1 (echo "not exists") else (echo "exists")

或作为快捷方式:

where /q mingw32-make && echo found || echo not found