Check If an Executable Exists in the Windows Path

Check if an executable exists in the Windows path

I think there's nothing built-in, but you could do something like this with System.IO.File.Exists:

public static bool ExistsOnPath(string fileName)
{
return GetFullPath(fileName) != null;
}

public static string GetFullPath(string fileName)
{
if (File.Exists(fileName))
return Path.GetFullPath(fileName);

var values = Environment.GetEnvironmentVariable("PATH");
foreach (var path in values.Split(Path.PathSeparator))
{
var fullPath = Path.Combine(path, fileName);
if (File.Exists(fullPath))
return fullPath;
}
return null;
}

How to test if an executable exists in the %PATH% from a windows batch file?

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.

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.

Programmatically check if executable exists without running it or using `which`

Looks like you want findExecutable function from here.

Prelude System.Directory> findExecutable "gcc"
Just "/usr/bin/gcc"

Test if executable exists in Python?

Easiest way I can think of:

def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file

return None

Edit: Updated code sample to include logic for handling case where provided argument is already a full path to the executable, i.e. "which /bin/ls". This mimics the behavior of the UNIX 'which' command.

Edit: Updated to use os.path.isfile() instead of os.path.exists() per comments.

Edit: path.strip('"') seems like the wrong thing to do here. Neither Windows nor POSIX appear to encourage quoted PATH items.

Test if executable is in path in PowerShell

You can test through Get-Command (gcm)

if (Get-Command "pandoc.exe" -ErrorAction SilentlyContinue) 
{
pandoc -Ss readme.txt -o readme.html
}

If you'd like to test the non-existence of a command in your path, for example to show an error message or download the executable (think NuGet):

if ((Get-Command "pandoc.exe" -ErrorAction SilentlyContinue) -eq $null) 
{
Write-Host "Unable to find pandoc.exe in your PATH"
}

Try

(Get-Help gcm).description

in a PowerShell session to get information about Get-Command.

Find if executable is available in PATH

I came up with this:

Set wshShell = WScript.CreateObject ("WSCript.shell")
On Error Resume Next
wshshell.run "your_exec", 6, True
If Err.Number <> 0 Then
WshShell.Popup(Err.Number)
' Handle error
Err.Clear
End If
On Error Goto 0
set wshshell = nothing

The your_exec must be a call to the executable made in such a way that would return immediately, which could be a problem if that executable doesn't provide some command line option to do so. In my case I'm just calling the executable to show its version: my.exe -version.

How to check if an executable command exists in R

If it is a command in PATH, then it is actually a file somewhere in the search path.

An easy way is

system("which <command>")  # for unix
system("where <command>") # for windows

If the command exists, then this should show the full path. Otherwise nothing.



Related Topics



Leave a reply



Submit