How to check if an executable exists in% PATH% from a Windows batch file?

I am looking for an easy way to check if an executable exists in the PATH environment variable from a Windows batch file.

The use of external tools not provided by the OS is not allowed. The minimum version of Windows is Windows XP.

+78
windows batch-file batch-processing
Jan 24 '11 at 12:08
source share
8 answers
for %%X in (myExecutable.exe) do (set FOUND=%%~$PATH:X) if defined FOUND ... 

If you need this for different extensions, just PATHEXT :

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

Maybe this is so, where also already exists on older versions of Windows, but I donโ€™t have access to one, so I canโ€™t say. The following also works on my machine:

 where myExecutable 

and returns with a non-zero exit code if it cannot be found. In the package, you probably also want to redirect the output to NUL .

Keep in mind

Parsing in batch ( .bat ) files and on the command line is different (because batch files have %0 - %9 ), so you have to double % there. This is not necessary on the command line, so just %X for variables

+62
Jan 24 '11 at 12:10
source share

Windows Vista and later come with a program called where.exe , which looks for programs on the way. It works as follows:

 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 simply sets ERRORLEVEL and produces no output.

 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 go! ) 

Or a simple (but less readable) shortened version that prints a message and exits your application:

 where /q myapplication || ECHO Cound not find app. && EXIT /B 
+59
Sep 06 '14 at 3:08
source share

Here is a simple solution that tries to run the application and after that handle any error.

 file.exe /? 2> NUL IF NOT %ERRORLEVEL%==9009 ECHO file.exe exists in path 

Error code 9009 usually means that the file was not found.

The only drawback is that file.exe actually executes if found (which in some cases is not possible).

+18
Nov 04 '13 at
source share

This can be achieved by changing the parameters.

 %~$PATH:1 

This returns the full path to the executable file name in% 1, otherwise an empty string.

This does not work with user variables. So if the name of the executable is not a parameter of your script, then you need a routine. For example:

 call :s_which app.exe if not "%_path%" == "" ( "%_path%" ) goto :eof :s_which setlocal endlocal & set _path=%~$PATH:1 goto :eof 

See http://ss64.com/nt/syntax-args.html.

+4
Sep 24 '13 at 13:03
source share
 @echo off set found= set prog=cmd.exe for %%i in (%path%) do if exist %%i\%prog% set found=%%i echo "%found%" if "%found%"=="" .... 
0
Jan 25 '11 at 16:50
source share

Sometimes this simple solution works when you check to see if the result matches what you expect. The first line starts the command and captures 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. 
0
Jun 12 '13 at 16:03
source share

Use command: powershell Test-Path "exe you're looking for"

It will return True if it is present, otherwise False.

0
Jun 26 '15 at 12:04 on
source share

For those looking for a PowerShell option. You can use the Get-Command cmdlet by passing two elements. First, indicate the current location of the directory with the prefix .\ , Then specify only the name of the executable file.

 (Get-Command ".\notepad", "notepad" -ErrorAction Ignore -CommandType Application) -ne $null 

This will return true if found locally or in system-wide paths.

0
Jan 31 '19 at 18:18
source share



All Articles