29

What is the best way to find out where notepad.exe and mspaint.exe are that will work across various versions of Windows?

Should I get the Windows directory via SHGetFolderPath(NULL, CSIDL_WINDOWS, NULL, SHGFP_TYPE_CURRENT, dir), and then traverse through all the subdirectories to look for the two files?

(Assume that I am not interested in anything outside the Windows folder.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Heng-Cheong Leong
  • 824
  • 1
  • 8
  • 13
  • Whatever solution you decide on, be prepared to *not* find them at all. I have deleted either or both on some systems (why should I keep notepad.exe around when I have notepad++) – Stephen P Aug 07 '10 at 00:06
  • 9
    @Stephen: What's the point in deleting such lightweight little things which won't really release any resources but just introduce the risk of breaking some programs? – chiccodoro Sep 30 '10 at 13:42

11 Answers11

41

This works on every Windows box I've got access to (XP+).

c:\> for %i in (cmd.exe) do @echo %~$PATH:i
C:\WINDOWS\system32\cmd.exe

c:\> for %i in (python.exe) do @echo %~$PATH:i
C:\Python25\python.exe

The great thing is, you don't have to use the actual %PATH%, you can substitute your own search path by using a different environment variable.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • It wouldn't on many localized versions of Windows as the executable might be named differently. – peSHIr Feb 06 '09 at 07:30
  • Then you substitute the localized name. This will be a problem with any solution unless you think there's a WIN_NOTEPAD_REALNAME constant somewhere in the Win32 API. – paxdiablo Feb 06 '09 at 07:37
  • 5
    notepad.exe is still notepad.exe and cmd.exe is still cmd.exe in all the language versions of Windows (98,XP,Vista) I've seen (about 20). Folders may be named differently, but executables, libraries, and other important stuff are not translated - at least not the filenames. – Piskvor left the building Feb 06 '09 at 14:39
  • I don't think this works when the filename is in multiple paths, 'cos it doesn't seem to display all the paths.. for example, tail.exe i've got in a windows resource kit, and as part of gnuwin32 and in cygwin\bin so 3 places, but it only displays one. – barlop Aug 14 '11 at 18:49
  • @barlop, it mirrors what Windows will do when you type in `tail.exe` - that will only run the first copy (in terms of your path), not all three. – paxdiablo Aug 14 '11 at 23:02
  • what is the significance of `.` in `@echo.` ? – Miserable Variable Jan 12 '12 at 20:05
  • 1
    @MiserableVariable, under (old) DOS from memory, if you typed `echo hello` (with many spaces between the words, those spaces were _ignored_ and you just got "hello". If you used the dot variant, it would output the spaces between the dot and the word as well. XP (at least) seems to have change that so that the _first_ space (or dot) is ignored, the others are preserved. Force of habit on the part of an aging IT guy :-) Unnecessary here so I'll get rid of it. – paxdiablo Jan 13 '12 at 00:27
  • Could someone explain how this works? I don't understand the meaning of "%~$PATH:i" – Nick Garvey Jan 19 '12 at 16:47
19

If you have the Microsoft Platform SDK installed (the February 2003 version is the last one that works with Microsoft VC6), you can grab the where.exe program (it's 38K, only 18K if you gzip it) and run

where notepad.exe

help from the where command:

WHERE [/R dir] [/Q] [/F] [/T] pattern...

Description:
    Displays the location of files that match the search pattern.
    By default, the search is done along the current directory and
    in the paths specified by the PATH environment variable.

Parameter List:
    /R       Recursively searches and displays the files that match the
             given pattern starting from the specified directory.

    /Q       Returns only the exit code, without displaying the list
             of matched files. (quite mode)

    /F       Displays the matched filename in double quotes.

    /T       Displays the file size, last modified date and time for all
             matched files.

    pattern  Specifies the search pattern for the files to match.
             Wildcards * and ? can be used in the pattern. The
             "$env:pattern" and "path:pattern" formats can also be
             specified, where "env" is an environment variable and
             the search is done in the specified paths of the "env"
             environment variable. These formats should not be used
             with /R. The search is also done by appending the
             extensions of the PATHEXT variable to the pattern.

     /?      Displays this help message.

  NOTE: The tool returns an error level of 0 if the search is
        successful, of 1 if the search is unsuccessful and
        of 2 for failures or errors.

Examples:
    WHERE /?
    WHERE myfilename1 myfile????.*
    WHERE $windir:*.*
    WHERE /R c:\windows *.exe *.dll *.bat
    WHERE /Q ??.???
    WHERE "c:\windows;c:\windows\system32:*.dll"
    WHERE /F /T *.dll
Jason S
  • 184,598
  • 164
  • 608
  • 970
3

Check if the key HKEY_CLASSES_ROOT\Applications\notepad.exe is the same on localized versions. Maybe the key name is same and the value for edit/open points to the localized exe.
Example:

English:
HKEY_CLASSES_ROOT\Applications\notepad.exe\shell\edit\command
%SystemRoot%\system32**NOTEPAD.EXE** %1

Dutch:
HKEY_CLASSES_ROOT\Applications\notepad.exe\shell\edit\command
%SystemRoot%\system32**kladblok.exe** %1

If thats the case, then its just about to check the registry for that key (same goes for the mspaint).

Stefan
  • 11,423
  • 8
  • 50
  • 75
  • Sounds like a viable way of doing this, yes. Provided that the assumption holds. (Can't check myself, as I'm not on a non-English localized Windows and I don't have registry editing permissions here...) – peSHIr Feb 06 '09 at 08:40
  • Anyone else that has possibility to check? – Stefan Feb 06 '09 at 08:46
  • at least in the german version the key is the same, however notepad.exe still is called notepad.exe – Alex Mar 30 '15 at 13:46
3

Type:

%windir%\system32\notepad.exe

in the path bar thingy.

Or

C:\Windows\System32

and find notepad.exe. C is the hard drive where your OS is located.

Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30
LukasFT
  • 164
  • 2
  • 10
2

I think to start off small you should get the windir environment variable and look in the subfolders %windir%\system32\ for mspaint and notepad. Most likely they will be there.

However if that fails, well then resort to a more brute force search.

RomanM
  • 6,363
  • 9
  • 33
  • 41
2

Normally, you would just execute them. They are on the system path in every version of Windows.

You can use ExpandEnvironmentStrings. The environment variable you want to expand is WINDIR.

In the past you could have used GetWindowsDirectory or GetSystemDirectory, but I think they are deprecated.

Deanna
  • 23,876
  • 7
  • 71
  • 156
Bogdan
  • 3,055
  • 1
  • 22
  • 20
1

Use the WinAPI function GetWindowsDirectory() to get the Windows folder, and GetSystemDirectory() to get the Windows\System folder. Thely're guaranteed to work with all Windows versions since at least Win95; I think they were available in Win 3.x as well.

Ken White
  • 123,280
  • 14
  • 225
  • 444
1

Since you tagged the question with WinAPI, I'd use SearchPath() e.g. the following will populate the variable path with the result.

//Get the full path to notepad
char path[MAX_PATH] = { 0 };
LPSTR* ptr = NULL;
DWORD dwRet = SearchPath(NULL, "notepad.exe", NULL, MAX_PATH, (LPSTR)path, ptr);
opticyclic
  • 7,412
  • 12
  • 81
  • 155
nabiy
  • 359
  • 2
  • 4
0

In short I find that the best approach is to check the Windows\System32 directory and the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths registry keys.

More generally I find that the best approach is to mimic ShellExecuteEx.

Taken from:
Application Registration (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx

The file is sought in the following locations:
•The current working directory.
•The Windows directory only (no subdirectories are searched).
•The Windows\System32 directory.
•Directories listed in the PATH environment variable.
•Recommended: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

A further possibly is to check Start Menu\Programs\Accessories, by using SHGetFolderPath with CSIDL_STARTMENU := 11 and CSIDL_COMMON_STARTMENU := 22, and retrieve the targets from the lnk files.

vafylec
  • 965
  • 1
  • 6
  • 23
0

Try opening a DOS prompt, change to the Windows folder and do:

dir notepad.exe /s

Long live DOS :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tehvan
  • 10,189
  • 5
  • 27
  • 31
-2

Go to the system32 folder and type "notepad.exe" into the 'File Name' bar.

Kelly
  • 1