tags:

views:

169

answers:

6

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. :-)

+3  A: 

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

HTH

Thank you - short but sweet, I knew there was a simple command-line tool for it...!
kronoz
For what OS? I can't find "where.exe" anywhere on my Windows XP system.
Patrick Cuff
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
+1  A: 

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.)

Kris
The tag stated windows.
jop
I know now, but didn't see the tag before I posted
Kris
this is still useful however, thanks! I do use unix on occasion.
kronoz
A: 

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

 function PSwhere($file) { $env:Path.Split(";") | ? { test-path $_\$file* } }
Scott Weinstein
A: 

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'.

Richard T
A: 

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.

Nick
+1  A: 

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.

Patrick Cuff
+1 one of the few places the search engines came up with a solution that works without any 3rd party tools. thanks!
Jeroen Pluimers
you can replace $PATH with any $environmentVariable, and also look for non-binary files. Just what I needed!
Jeroen Pluimers