如何测试如果可执行文件在%PATH%存在从Windows批处理文件? [英] How to test if an executable exists in the %PATH% from a windows batch file?

查看:92
本文介绍了如何测试如果可执行文件在%PATH%存在从Windows批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个简单的方法来测试,如果一个可执行文件从Windows批处理文件中的PATH环境变量存在。

的未由OS提供的外部工具的使用是不被允许的。所需的最低Windows版本是Windows XP中。


解决方案

 在(myExecutable.exe)%% X千万台(套FOUND = %%〜$ PATH:X)
如果定义FOUND ...

如果您需要这个不同的扩展名,只是遍历 PATHEXT

 集中找到=
在(%PATHEXT%)%% e执行(
  对于%%中X(myExecutable %% E)做(
    如果没有定义FOUND(
      集中找到= %%〜$ PATH:X
    )
  )

会不会是其中,也存在已经在旧的Windows版本,但我没有获得一个,所以我不能告诉。在我的机器下面也可以工作:

 其中myExecutable

和一个非零的退出code返回,如果它不能被发现。在批处理,你可能还需要将输出重定向到 NUL ,虽然

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 the OS is not allowed. The minimal Windows version required is Windows XP.

解决方案

for %%X in (myExecutable.exe) do (set FOUND=%%~$PATH:X)
if defined FOUND ...

If you need this for different extensions, just iterate over PATHEXT:

set FOUND=
for %%e in (%PATHEXT%) do (
  for %%X in (myExecutable%%e) do (
    if not defined FOUND (
      set FOUND=%%~$PATH:X
    )
  )
)

Could be that where also exists already on legacy Windows versions, but I don't have access to one, so I cannot tell. On my machine the following also works:

where myExecutable

and returns with a non-zero exit code if it couldn't be found. In a batch you probably also want to redirect output to NUL, though.

这篇关于如何测试如果可执行文件在%PATH%存在从Windows批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆