检查命令在批处理文件中是否可用 [英] Check whether command is available in batch file
问题描述
我正在为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屋!