181

Say I have a program X.EXE installed in folder c:\abcd\happy\ on the system. The folder is on the system path. Now suppose there is another program on the system that's also called X.EXE but is installed in folder c:\windows\.

Is it possible to quickly find out from the command line that if I type in X.EXE which of the two X.EXE's will get launched? (but without having to dir search or look at the process details in Task Manager).

Maybe some sort of in-built command, or some program out there that can do something like this? :

detect_program_path X.EXE
1

3 Answers 3

319

Use the where command. The first result in the list is the one that will execute.

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

According to this blog post, where.exe is included with Windows Server 2003 and later, so this should just work with Vista, Win 7, et al.

On Linux, the equivalent is the which command, e.g. which ssh.

Sign up to request clarification or add additional context in comments.

8 Comments

+1 ! I never knew this could have been a part of Windows and so didn't look in that direction! :)
Any equivalent for the poor xp users?
@Kenny: Assuming you know the name of the DLL, you can use the ListDLLs utility (technet.microsoft.com/en-us/sysinternals/bb896656). From the command-line, just run listdlls -d foo.dll to see all processes that have the module loaded and the full path to the loaded module. Alternatively, you can just do a Windows file search for the file name.
@Kenny: What you are asking for is very different from the answer posted here. You should create a new Stack Overflow question with what research you've done and post a link to it in these comments.
In case you are executing it from Powershell, remember to use where.exe as where is alias for other Powershell command, thus will not execute what you want (output will be empty).
|
30

As the thread mentioned in the comment, get-command in powershell can also work it out. For example, you can type get-command npm and the output is as below:

enter image description here

2 Comments

(get-command npm).Source will respond with just the path to npm (for the example) instead of the whole table of all the properties.
I was looking for info on dir and Get-ChildItem and where is not helpful for those. This works though!
11

Here's a little cmd script you can copy-n-paste into a file named something like where.cmd:

@echo off
rem - search for the given file in the directories specified by the path, and display the first match
rem
rem    The main ideas for this script were taken from Raymond Chen's blog:
rem
rem         http://blogs.msdn.com/b/oldnewthing/archive/2005/01/20/357225.asp
rem
rem
rem - it'll be nice to at some point extend this so it won't stop on the first match. That'll
rem     help diagnose situations with a conflict of some sort.
rem

setlocal

rem - search the current directory as well as those in the path
set PATHLIST=.;%PATH%
set EXTLIST=%PATHEXT%

if not "%EXTLIST%" == "" goto :extlist_ok
set EXTLIST=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
:extlist_ok

rem - first look for the file as given (not adding extensions)
for %%i in (%1) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i

rem - now look for the file adding extensions from the EXTLIST
for %%e in (%EXTLIST%) do @for %%i in (%1%%e) do if NOT "%%~$PATHLIST:i"=="" echo %%~$PATHLIST:i

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.