[ACCEPTED]-Locating a file on the path-windows

Accepted answer
Score: 49

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

0

Score: 6

For WindowsNT-based systems:

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

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

Score: 3

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

Score: 1

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

Your questions title 8 doesn't specify windows so I imagine some 7 folks might find this question looking for 6 the same with a posix OS on their mind (like 5 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 4 is slightly altered production code I'm 3 running on several servers. (i.e. taken 2 out of OO context and left some sanitation 1 and error checking out for brevity.)

Score: 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 Chad@ChadsTech.net - 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 1 the answer with some details over at http://blogs.catapultsystems.com/chsimmons/archive/2018/01/30/parse-envpath-with-powershell

Score: 0

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

I also added 11 a feature that has turned out to be very 10 nifty; the -s flag tells it to search not 9 just the system path, but everything on 8 the system disk, known user-directories 7 excluded. I have found this feature to be 6 incredibly useful in systems administration 5 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 4 such a utility is not hard and I'll leave 3 it as an exercise for the reader. Or, if 2 asked here, I'll post my script - its in 1 'bash'.

More Related questions