which.exe - The best of Windows where.exe and Unix which, and some more
Posted: 26 Mar 2019 12:54
[Edited after each new release, to update the version history, and list the features in the latest version]
Announcing which.exe, a tool for searching which programs will run at the command.com, cmd.exe, or PowerShell prompts.
This which.exe program strives to implement the best of both Unix which and Windows where.exe, and a bit more!
Features:
which.exe is available as part of the latest System Tools Library release: SysTools.zip
Note that the zip file contains multiple versions of which.exe: The one at the root, which runs in all MS OSs, as explained above; And several stripped down versions, that run just in DOS, or in particular versions of Windows respectively (WIN32=x86/WIN64=amd64/ARM=arm32/ARM64=arm64).
It is also possible to build it from source for Unix (Linux, MacOS, FreeBSD, etc). The Linux version is usable in the Windows 10 LinuX SubSystem, or in any distribution of Linux. Caution: Unix builds output an executable named 'Which', with a capital W. Do not rename it to 'which', to avoid overwriting the original Linux 'which' from your Linux distribution!
To display a help screen with a full list of options, run:
Note about the option -i introduced in version 1.12, for listing cmd doskey aliases:
As discussed in this thread, capturing the doskey macros for the current cmd shell is tricky.
Piping the 'doskey.exe /macros' output to 'which.exe -i' will not work. It is necessary to use a doskey macro for which itself:
And the best way to define doskey macros in an extensible way is to use the AutoRun.cmd script.
See this post on this forum for explanations on how to use AutoRun.cmd, and how it works.
For PowerShell, the problem is very similar, and it is necessary to define a which function invoking which.exe.
Run 'which -?' to get help on how to configure both shells for this to work.
The source code is available on GitHub in the SysToolsLib repository: which.c
Use the version in the zip file above to get the latest which.exe features documented here.
If you find any problem with this which.exe, please report it on GitHub in SysToolsLib issues.
Versions history:
Announcing which.exe, a tool for searching which programs will run at the command.com, cmd.exe, or PowerShell prompts.
This which.exe program strives to implement the best of both Unix which and Windows where.exe, and a bit more!
Features:
- Allows passing just the base name of a command, and uses the PATHEXT environment variable to infer possible extensions.
(Like Windows' own where.exe, but unlike all Windows ports of Unix which.) - Uses Unicode internally, and outputs file pathnames correctly in any code page. (Provided that the console font has the necessary characters!)
(Again, Windows ports of Unix which all fail at that if a pathname contains non-ASCII characters.) - Knows about command.com, cmd.exe, and PowerShell specific rules. For example, the latter searches for *.ps1 before trying extensions from PATHEXT.
Also command.com and cmd.exe search in ".", whereas PowerShell does not. (Both where.exe and Windows ports of Unix which fail at that!) - Recognizes UWP applications execution links, and displays their target.
- By default outputs only the first match. Use option -a to display all matching programs. (Like Unix which)
- A -i option allows to efficiently detect cmd.exe doskey macros, and PowerShell aliases.
Run 'which -?' to get help on how to configure both shells for this to work.
(where.exe has no equivalent; Unix which has this option for Unix shells, but Windows ports don't support that in Windows.) - A unique -l option displays the program(s) date/time and size; Resolves symbolic links; etc.
- A unique -v option displays verbose comments about why some seemingly obvious programs will NOT run.
For example in Vista and later, with cmd.exe variable NoDefaultCurrentDirectoryInExePath set, cmd does not search in ".". In Linux, it'll report name case errors. - A unique ability to search for names with wildcards. This is useful when you don't quite remember the exact name of a program.
- Avoids listing twice the same executable when a path appears multiple times in the PATH.
- Last, but not least, a single .exe that runs in all versions of DOS and Windows, from DOS 3 16-bits to Windows 10 64-bits. Including Windows 95 and XP.
which.exe is available as part of the latest System Tools Library release: SysTools.zip
Note that the zip file contains multiple versions of which.exe: The one at the root, which runs in all MS OSs, as explained above; And several stripped down versions, that run just in DOS, or in particular versions of Windows respectively (WIN32=x86/WIN64=amd64/ARM=arm32/ARM64=arm64).
It is also possible to build it from source for Unix (Linux, MacOS, FreeBSD, etc). The Linux version is usable in the Windows 10 LinuX SubSystem, or in any distribution of Linux. Caution: Unix builds output an executable named 'Which', with a capital W. Do not rename it to 'which', to avoid overwriting the original Linux 'which' from your Linux distribution!
To display a help screen with a full list of options, run:
Code: Select all
which -?
Note about the option -i introduced in version 1.12, for listing cmd doskey aliases:
As discussed in this thread, capturing the doskey macros for the current cmd shell is tricky.
Piping the 'doskey.exe /macros' output to 'which.exe -i' will not work. It is necessary to use a doskey macro for which itself:
Code: Select all
doskey /macros which=^(help ^& doskey /macros^) ^| which.exe -i $*
See this post on this forum for explanations on how to use AutoRun.cmd, and how it works.
For PowerShell, the problem is very similar, and it is necessary to define a which function invoking which.exe.
Run 'which -?' to get help on how to configure both shells for this to work.
The source code is available on GitHub in the SysToolsLib repository: which.c
Use the version in the zip file above to get the latest which.exe features documented here.
If you find any problem with this which.exe, please report it on GitHub in SysToolsLib issues.
Versions history:
- 1.16 2020-12-16 Recognize pwsh.exe as PowerShell. Option -l also shows the executable size. Supports UWP App. Exec. Links.
- 1.15 2020-04-20 Internal changes for MacOS support. Features otherwise unchanged.
- 1.14 2019-09-26 Added the ability to search names with wildcards. Added a verbose msg about case-independent matches in Unix.
- 1.13 2019-06-22 Avoid searching twice in the same directory. Added program properties, visible in the .exe properties box Details tab.
- 1.12 2019-03-01 Added a -i option allows to efficiently detect cmd.exe doskey macros, and PowerShell aliases.
- 1.11 2019-01-16 Added option -- to stop processing switches. Allows searching for names that begin with a -.
- 1.10 2018-03-22 Added option -l, and changed -v to display comments about programs excluded.
- ...
- 1.0 1987 Initial version for MS-DOS only