Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Due to the number of directories you can put into your %PATH% environment variable it's very easy to have unintentional conflicts between application names. I swore I've come across this tip before, but I can't locate it anywhere. Is there way way at the command prompt to see the directory where the application is located in? For example, I'm looking for something like the following:

C:\> xcopy
Invalid number of parameters
0 File(s) copied

C:\> **SOME_COMMAND** xcopy
C:\Windows\System32\xcopy.exe
share|improve this question
    
If I understand correctly, you're looking for something similar to the GNU "which" command. –  Moshe Nov 30 '09 at 22:28

5 Answers 5

up vote 1 down vote accepted

A little heavy, but if you like the command line in windows you owe it to yourself to try Cygwin. Place it's \bin folder on your PATH and you'll have a whole host of great tools, not least which, which tells you exactly which file you'd be executing :)

(And yes, I realise the irony of adding to your PATH to help with having too much there)

edit: This may also be appropriate.

share|improve this answer
    
which! That's the command I was thinking of. Cygwin would be entire overkill for what I'm looking for though. Even the full Unix Tools for Windows would be more than I need. I could have sworn there was an equivalent which command specific for Windows though. –  Agent_9191 Nov 30 '09 at 17:49

Where.exe is the Windows equivalent of whereis on unices. If you are using Windows Server 2003+ or Vista+, this tool installs to %windir%/System32. Using "where" in the command shell (cmd.exe) shows results in path search order:

C:\> where where
C:\Windows\System32\where.exe

Keep in mind that PowerShell typically aliases 'where' to Where-Object, so you just need to include '.exe' when calling where from PowerShell:

C:\> where.exe where
C:\Windows\System32\where.exe

Also see Windows equivalent of whereis?

share|improve this answer

Here is a simple bath file which will do what you want.

@for %%e in (%PATHEXT%) do @for %%i in (%1%%e) do @if NOT "%%~$PATH:i"=="" echo %%~$PATH:i

whereis xcopy - works whereis xcopy.exe - will not work

Hope it helps (somewhat)

Regards

share|improve this answer
    
Almost forgot to factor in the PATHEXT variable, thanks. But this is limited in the file location and extension that it looks for. So it doesn't quite fit what I was looking for, but it definitely gave me a great jumping off point. –  Agent_9191 Nov 30 '09 at 17:54

Based on both Mick's answer and Phoshi's I came up with the solution I was looking for. I created an AutoIt script that loops through all the PATHEXT's and PATH's to find the first location where the passed parameter is located at. It handles both extension-less as well as extension-full parameters. It first searches the local working directory, then the environment variables. As far as everything I've read, this is how Windows performs it's lookup for the command as well. Here's the script:

If $CmdLine[0] > 0 Then
    $commandToFind = $CmdLine[1]
    $matchFound = false
    $foundPath = ""
    $pathEnvironmentVariable = EnvGet("PATH")
    $pathDirectories = StringSplit($pathEnvironmentVariable, ";", 2)
    $pathExtensionsEnvironmentVariable = EnvGet("PATHEXT")
    $pathExtensions = StringSplit($pathExtensionsEnvironmentVariable, ";", 2)

    ; Search the local directory first for the file
    If FileExists($commandToFind) Then
    	$matchFound = true
    	$foundPath = @WorkingDir & "\" & $commandToFind
    EndIf

    For $pathExtension In $pathExtensions
    	$fullPath = @WorkingDir & "\" & $commandToFind & StringLower($pathExtension)
    	If FileExists($fullPath) Then
    		$matchFound = true
    		$foundPath = $fullPath
    		ExitLoop
    	EndIF
    Next

    If Not $matchFound == true Then
    	; Loop through all the individual directories located in the PATH environment variable
    	For $pathDirectory In $pathDirectories
    		If FileExists($pathDirectory) Then
    			$pathWithoutExtension = $pathDirectory & "\" & $commandToFind

    			; Check if the command exists in the current path. Most likely the parameter had the extension passed in
    			If FileExists($pathWithoutExtension) Then
    				$matchFound = true
    				$foundPath = $pathWithoutExtension
    				ExitLoop
    			EndIf

    			If Not $matchFound == true Then
    				; Loop through all possible system extensions to see if the file exists.
    				For $pathExtension In $pathExtensions
    					$fullPath = $pathWithoutExtension & StringLower($pathExtension)
    					If FileExists($fullPath) Then
    						$matchFound = true
    						$foundPath = $fullPath
    						ExitLoop
    					EndIf
    				Next
    			EndIf

    			If $matchFound == true Then
    				ExitLoop
    			EndIf
    		EndIf
    	Next
    EndIf
    If $matchFound == true Then
    	ConsoleWrite("Located at " & $foundPath & @CRLF)
    Else
    	ConsoleWriteError("Unable to locate the command" & @CRLF)
    EndIf
EndIf

To get it to work, you need to run it through the Aut2exe.exe program that comes with AutoIt (shortcut is titled Compile Script to .exe) and check the "Console?" checkbox when compiling it. I just compile it to a file called "which.exe" that I toss in an existing path directory (like C:\Windows\System32).

share|improve this answer

The UNIX GNU tools are available in Windows ports, you do not have to go through the pain of scripting it as above. The which command works when given xcopy or xcopy.exe or other executables including BAT files. These can be downloaded from:

http://gnuwin32.sourceforge.net/packages.html

They are very handy.

share|improve this answer
    
while I'm satisfied with what I came up with, this is a good resource for the other tools as well. –  Agent_9191 Nov 30 '09 at 22:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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