19

Does anybody know how to determine the location of a file that's in one of the folders specified by the PATH environmental variable other than doing a dir filename.exe /s from the root folder?

I know this is stretching the bounds of a programming question but this is useful for deployment-related issues, also I need to examine the dependencies of an executable. :-)

7 Answers 7

51

You can use the where.exe utility in the C:\Windows\System32 directory.

3
  • Thank you - short but sweet, I knew there was a simple command-line tool for it...!
    – ljs
    Oct 12, 2008 at 15:20
  • For what OS? I can't find "where.exe" anywhere on my Windows XP system. Oct 13, 2008 at 12:18
  • 1
    WHERE.EXE ships with Windows XP Server 2003 and up as well as the Windows resource kits since Win2K. It's also included with VS2005, but not 2008 (C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin\Where.Exe).
    – raven
    Oct 13, 2008 at 18:42
6

For WindowsNT-based systems:

for %i in (file) do @echo %~dp$PATH:i

Replace file with the name of the file you're looking for.

3
  • +1 one of the few places the search engines came up with a solution that works without any 3rd party tools. thanks! Oct 19, 2009 at 8:28
  • you can replace $PATH with any $environmentVariable, and also look for non-binary files. Just what I needed! Oct 19, 2009 at 8:29
  • 1
    Use for %e in (%PATHEXT%) do @(for %x in (file%e) do @if not "%~$PATH:x"=="" @echo.%~$PATH:x) if you don't know the extension.
    – Joey
    Apr 1, 2011 at 10:15
4

If you want to locate the file at the API level, you can use PathFindOnPath. It has the added bonus of being able to specify additional directories, in case you want to search in additional locations apart from just the system or current user path.

1

On windows i'd say use %WINDIR%\system32\where.exe

Your questions title doesn't specify windows so I imagine some folks might find this question looking for the same with a posix OS on their mind (like myself).

This php snippet might help them:

<?php
function Find( $file )
{
    foreach( explode( ':', $_ENV( 'PATH' ) ) as $dir )
    {
        $command = sprintf( 'find -L %s -name "%s" -print', $dir, $file );
        $output  = array();
        $result  = -1;
        exec( $command, $output, $result );

        if ( count( $output ) == 1 )
        {
            return( $output[ 0 ] );
        }
    }
    return null;
}
?>

This is slightly altered production code I'm running on several servers. (i.e. taken out of OO context and left some sanitation and error checking out for brevity.)

2
  • I know now, but didn't see the tag before I posted
    – Kris
    Oct 12, 2008 at 15:50
  • this is still useful however, thanks! I do use unix on occasion.
    – ljs
    Oct 12, 2008 at 16:44
1

Using PowerShell on Windows...

Function Get-ENVPathFolders {
#.Synopsis Split $env:Path into an array
#.Notes 
#  - Handle 1) folders ending in a backslash 2) double-quoted folders 3) folders with semicolons 4) folders with spaces 5) double-semicolons i.e. blanks
#  - Example path: 'C:\WINDOWS\;"C:\Path with semicolon; in the middle";"E:\Path with semicolon at the end;";;C:\Program Files;
#  - 2018/01/30 by [email protected] - Created
$NewPath = @()
$env:Path.ToString().TrimEnd(';') -split '(?=["])' | ForEach-Object { #remove a trailing semicolon from the path then split it into an array using a double-quote as the delimeter keeping the delimeter
    If ($_ -eq '";') { # throw away a blank line
    } ElseIf ($_.ToString().StartsWith('";')) { # if line starts with "; remove the "; and any trailing backslash
        $NewPath += ($_.ToString().TrimStart('";')).TrimEnd('\')
    } ElseIf ($_.ToString().StartsWith('"')) {  # if line starts with " remove the " and any trailing backslash
        $NewPath += ($_.ToString().TrimStart('"')).TrimEnd('\') #$_ + '"'
    } Else {                                    # split by semicolon and remove any trailing backslash
        $_.ToString().Split(';') | ForEach-Object { If ($_.Length -gt 0) { $NewPath += $_.TrimEnd('\') } }
    }
}
Return $NewPath
}

$myFile = 'desktop.ini'
Get-ENVPathFolders | ForEach-Object { If (Test-Path -Path $_\$myFile) { Write-Output "Found [$_\$myFile]" } } 

I also blogged the answer with some details over at http://blogs.catapultsystems.com/chsimmons/archive/2018/01/30/parse-envpath-with-powershell

-1

just for kicks, here's a one-liner powershell implementation

 function PSwhere($file) { $env:Path.Split(";") | ? { test-path $_\$file* } }
1
  • That doesn't work, since paths in PATH can contain ; as well (and are quoted then).
    – Joey
    Apr 1, 2011 at 10:22
-1

In addition to the 'which' (MS Windows) and 'where' (unix/linux) utilities, I have written my own utility which I call 'findinpath'. In addition to finding the executable that would be executed, if handed to the command line interpreter (CLI), it will find all matches, returned path-search-order so you can find path-order problems. In addition, my utility returns not just executables, but any file-specification match, to catch those times when a desired file isn't actually executable.

I also added a feature that has turned out to be very nifty; the -s flag tells it to search not just the system path, but everything on the system disk, known user-directories excluded. I have found this feature to be incredibly useful in systems administration tasks...

Here's the 'usage' output:

usage: findinpath [ -p <path> | -path <path> ] | [ -s | -system ] <file>
   or  findinpath [ -h | -help ]

where: <file> may be any file spec, including wild cards

       -h or -help returns this text

       -p or -path uses the specified path instead of the PATH environment variable.

       -s or -system searches the system disk, skipping /d /l/ /nfs and /users

Writing such a utility is not hard and I'll leave it as an exercise for the reader. Or, if asked here, I'll post my script - its in 'bash'.

1
  • 1
    Congrats about writing a lengthy post on your sophisticated tool, which nobody has available.
    – sstn
    Aug 17 at 6:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.