如何从 Windows 批处理文件测试 %PATH% 中是否存在可执行文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4781772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test if an executable exists in the %PATH% from a windows batch file?
提问by sorin
I'm looking for a simple way to test if an executable exists in the PATH environment variable from a Windows batch file.
我正在寻找一种简单的方法来测试 Windows 批处理文件中的 PATH 环境变量中是否存在可执行文件。
Usage of external tools not provided by the OS is not allowed. The minimal Windows version required is Windows XP.
不允许使用非操作系统提供的外部工具。所需的最低 Windows 版本是 Windows XP。
采纳答案by Joey
for %%X in (myExecutable.exe) do (set FOUND=%%~$PATH:X)
if defined FOUND ...
If you need this for different extensions, just iterate over PATHEXT
:
如果你需要这个用于不同的扩展,只需迭代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
在旧版 Windows 版本上也已经存在,但我无法访问,所以我无法判断。在我的机器上,以下也有效:
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.
如果找不到,则返回一个非零退出代码。不过,在批处理中,您可能还想将输出重定向到NUL
。
Keep in mind
记住
Parsing in batch (.bat
) files and on the command line differs (because batch files have %0
–%9
), so you have to double the %
there. On the command line this isn't necessary, so for variables are just %X
.
批处理 ( .bat
) 文件和命令行中的解析不同(因为批处理文件有%0
– %9
),因此您必须在%
那里加倍。在命令行上这不是必需的,所以对于变量来说只是%X
.
回答by Ryan Bemrose
Windows Vista and later versions ship with a program called where.exe
that searches for programs in the path. It works like this:
Windows Vista 和更高版本附带一个名为where.exe
搜索路径中程序的程序。它是这样工作的:
D:\>where notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe
D:\>where where
C:\Windows\System32\where.exe
For use in a batch file you can use the /q
switch, which just sets ERRORLEVEL
and doesn't produce any output.
要在批处理文件中使用,您可以使用/q
开关,它只设置ERRORLEVEL
而不产生任何输出。
where /q myapplication
IF ERRORLEVEL 1 (
ECHO The application is missing. Ensure it is installed and placed in your PATH.
EXIT /B
) ELSE (
ECHO Application exists. Let's go!
)
Or a simple (but less readable) shorthand version that prints the message and exits your app:
或者一个简单(但可读性较差)的速记版本,用于打印消息并退出您的应用程序:
where /q myapplication || ECHO Cound not find app. && EXIT /B
回答by eadmaster
Here is a simple solution that attempts to run the application and handles any error afterwards.
这是一个简单的解决方案,它尝试运行应用程序并在之后处理任何错误。
file.exe /? 2> NUL
IF NOT %ERRORLEVEL%==9009 ECHO file.exe exists in path
Error code 9009 usually means file not found.
错误代码 9009 通常表示未找到文件。
The only downside is that file.exe
is actually executed if found (which in some cases is not desiderable).
唯一的缺点是file.exe
如果找到则实际执行(在某些情况下这是不可取的)。
回答by Chris Noe
This can be accomplished via parameter substitution.
这可以通过参数替换来实现。
%~$PATH:1
This returns the full path of the executable filename in %1, else an empty string.
这将返回 %1 中可执行文件名的完整路径,否则返回空字符串。
This does not work with user-defined variables. So if the executable filename is not a parameter to your script, then you need a subroutine. For example:
这不适用于用户定义的变量。因此,如果可执行文件名不是脚本的参数,那么您需要一个子例程。例如:
call :s_which app.exe
if not "%_path%" == "" (
"%_path%"
)
goto :eof
:s_which
setlocal
endlocal & set _path=%~$PATH:1
goto :eof
回答by PabloG
@echo off
set found=
set prog=cmd.exe
for %%i in (%path%) do if exist %%i\%prog% set found=%%i
echo "%found%"
if "%found%"=="" ....
回答by bdombro
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.
回答by akkidukes
Use command : powershell Test-Path "exe which you looking for"
使用命令:powershell Test-Path "exe which you looking for"
It will return True if its present, otherwise False.
如果存在,它将返回 True,否则返回 False。
回答by John C
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.
对于那些寻找 PowerShell 选项的人。您可以使用Get-Command
传递两个项目的cmdlet。首先给出带有.\
前缀的当前目录位置,然后只给出 exe 名称。
(Get-Command ".\notepad", "notepad" -ErrorAction Ignore -CommandType Application) -ne $null
That will return true if found local or in system wide paths.
如果在本地或系统范围的路径中找到,这将返回 true。
回答by Zafer ATLI
If you are searching something like me on startup folder, should go folder. For example i search exe on startup folder and i use this code like
如果你在启动文件夹上搜索像我这样的东西,应该去文件夹。例如,我在启动文件夹中搜索 exe 并使用此代码
@echo off
cd C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
where /q program.exe
IF ERRORLEVEL 1 (
echo F | xcopy /Y /S /I /E "\programsetup\programsetup.exe"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\program.exe"
) ELSE (
ECHO Application exists. Let's go!
)