检查命令在批处理文件中是否可用 [英] Check whether command is available in batch file

查看:70
本文介绍了检查命令在批处理文件中是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Windows编写一个批处理文件,并使用命令7z(7-Zip).我已将其位置放在PATH中.有没有相对简单的方法来检查命令是否可用?

I'm writing a batch file for Windows and use the command 7z (7-Zip). I have put the location of it in the PATH. Is there a relatively easy way to check whether the command is available?

推荐答案

如果未找到命令,则尝试执行7z.exe将返回9009的%errorlevel%.您可以检查一下.

An attempt to execute 7z.exe will return an %errorlevel% of 9009 if the command is not found. You can check that.

7z.exe
if %errorlevel%==9009 echo Command Not Found

注意:此解决方案适用于此特定7zip用例,并且可能适用于许多其他用例.但作为一般规则,执行命令以确定其是否存在可能有害.因此,请确保您了解执行要检查的命令的效果,并谨慎使用此方法.

Note: This solution is viable for this specific 7zip use case, and likely for plenty of others. But as a general rule, executing a command to determine whether it's present could potentially be harmful. So make sure you understand the effect of executing the command you're checking for, and use your discretion with this approach.

这篇关于检查命令在批处理文件中是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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