Windows 命令行上是否有相当于“which”的词?
-
08-07-2019 - |
题
由于我有时会遇到路径问题,即我自己的 cmd 脚本之一被另一个程序(路径较早的)隐藏(隐藏),我希望能够在 Windows 命令行上找到程序的完整路径,给定只是它的名字。
是否有与 UNIX 命令“which”等效的命令?
在 UNIX 上, which command
打印给定命令的完整路径,以轻松查找和修复这些阴影问题。
解决方案
Windows Server 2003及更高版本(即Windows XP 32位之后的任何内容)提供where.exe
程序,该程序执行which
所做的一些操作,但它匹配所有类型的文件,而不仅仅是可执行命令。 (它与cd
等内置shell命令不匹配。)它甚至会接受通配符,因此where nt*
会查找名称以%PATH%
开头的nt
和当前目录中的所有文件。
尝试where /?
寻求帮助。
请注意,Windows PowerShell将where
定义为 Where-Object
的别名cmdlet ,因此如果您需要.exe
,则需要输入全名,而不是省略<=>扩展名。
其他提示
虽然Windows的更高版本具有where
命令,但您也可以使用环境变量修饰符在Windows 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
您不需要任何额外的工具,也不仅限于PATH
,因为您可以替换您希望使用的任何环境变量(当然是路径格式)。
而且,如果你想要一个可以处理PATHEXT中所有扩展的东西(就像Windows本身那样),那么这个就可以了:
@echo off
setlocal enableextensions enabledelayedexpansion
:: Needs an argument.
if "x%1"=="x" (
echo Usage: which ^<progName^>
goto :end
)
:: First try the unadorned filenmame.
set fullspec=
call :find_it %1
:: Then try all adorned filenames in order.
set mypathext=!pathext!
:loop1
:: Stop if found or out of extensions.
if "x!mypathext!"=="x" goto :loop1end
:: Get the next extension and try it.
for /f "delims=;" %%j in ("!mypathext!") do set myext=%%j
call :find_it %1!myext!
:: Remove the extension (not overly efficient but it works).
:loop2
if not "x!myext!"=="x" (
set myext=!myext:~1!
set mypathext=!mypathext:~1!
goto :loop2
)
if not "x!mypathext!"=="x" set mypathext=!mypathext:~1!
goto :loop1
:loop1end
:end
endlocal
goto :eof
:: Function to find and print a file in the path.
:find_it
for %%i in (%1) do set fullspec=%%~$PATH:i
if not "x!fullspec!"=="x" @echo. !fullspec!
goto :eof
它实际上返回了所有可能性,但您可以轻松地针对特定搜索规则进行调整。
在PowerShell下, Get-Command
将在$Env:PATH
中的任何位置找到可执行文件。
Get-Command eventvwr
CommandType Name Definition
----------- ---- ----------
Application eventvwr.exe c:\windows\system32\eventvwr.exe
Application eventvwr.msc c:\windows\system32\eventvwr.msc
它还可以找到PowerShell cmdlet,函数,别名,带有自定义可执行文件扩展名的文件,通过$Env:PATHEXT
等为当前shell定义(非常类似于Bash的type -a foo
) - 使其成为比其他工具更好的选择像where.exe
,which.exe
等,它们不知道这些PowerShell命令。
仅使用部分名称
查找可执行文件gcm *disk*
CommandType Name Version Source
----------- ---- ------- ------
Alias Disable-PhysicalDiskIndication 2.0.0.0 Storage
Alias Enable-PhysicalDiskIndication 2.0.0.0 Storage
Function Add-PhysicalDisk 2.0.0.0 Storage
Function Add-VirtualDiskToMaskingSet 2.0.0.0 Storage
Function Clear-Disk 2.0.0.0 Storage
Cmdlet Get-PmemDisk 1.0.0.0 PersistentMemory
Cmdlet New-PmemDisk 1.0.0.0 PersistentMemory
Cmdlet Remove-PmemDisk 1.0.0.0 PersistentMemory
Application diskmgmt.msc 0.0.0.0 C:\WINDOWS\system32\diskmgmt.msc
Application diskpart.exe 10.0.17... C:\WINDOWS\system32\diskpart.exe
Application diskperf.exe 10.0.17... C:\WINDOWS\system32\diskperf.exe
Application diskraid.exe 10.0.17... C:\WINDOWS\system32\diskraid.exe
...
查找自定义可执行文件
要查找其他非Windows可执行文件(python,ruby,perl等),需要将这些可执行文件的文件扩展名添加到PATHEXT
环境变量(默认为.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
)以识别具有这些扩展名的文件PATH
作为可执行文件。由于sal which gcm
也尊重此变量,因此可以将其扩展为列出自定义可执行文件。 e.g。
$Env:PATHEXT="$Env:PATHEXT;.dll;.ps1;.psm1;.py" # temporary assignment, only for this shell's process
gcm user32,kernel32,*WASM*,*http*py
CommandType Name Version Source
----------- ---- ------- ------
ExternalScript Invoke-WASMProfiler.ps1 C:\WINDOWS\System32\WindowsPowerShell\v1.0\Invoke-WASMProfiler.ps1
Application http-server.py 0.0.0.0 C:\Users\ME\AppData\Local\Microsoft\WindowsApps\http-server.py
Application kernel32.dll 10.0.17... C:\WINDOWS\system32\kernel32.dll
Application user32.dll 10.0.17... C:\WINDOWS\system32\user32.dll
您可以使用set-alias which get-command
(<=>的简短形式)快速设置别名。
有关更多信息和示例,请参阅 <=> 的在线帮助。
在Windows PowerShell中:
set-alias which where.exe
如果你安装了PowerShell(我推荐),你可以使用以下命令作为粗略的等价物(替换你的可执行文件名的programName):
($Env:Path).Split(";") | Get-ChildItem -filter programName*
更多信息在这里: 我的Manwich! PowerShell哪个
GnuWin32 工具有which
,还有一大堆其他Unix工具。
在Windows CMD中which
调用where
:
$ where php
C:\Program Files\PHP\php.exe
Cygwin 是一个解决方案。如果您不介意使用第三方解决方案,那么Cygwin就是您的选择。
Cygwin在Windows环境中为您提供了* nix的舒适感(您可以在Windows命令shell中使用它,或者使用您选择的* nix shell)。它为Windows提供了大量* nix命令(如which
),您可以在PATH
中包含该目录。
在 PowerShell 中,它是 gcm
, ,它提供有关其他命令的格式化信息。如果您只想检索可执行文件的路径,请使用 .Source
.
例如: gcm git
或者 (gcm git).Source
花絮:
- 适用于 Windows XP。
- 自 PowerShell 1.0 起可用。
gcm
是一个别名Get-Command
小命令.- 没有任何参数,它列出了主机 shell 提供的所有可用命令。
- 您可以使用以下命令创建自定义别名
Set-Alias which gcm
并像这样使用它:(which git).Source
. - 官方文档: https://technet.microsoft.com/en-us/library/ee176842.aspx
从这里获取unxutils: http://sourceforge.net/projects/unxutils/
Windows平台上的黄金,将所有漂亮的unix实用程序放在标准的Windows DOS上。多年来一直在使用它。它包含'哪个'。请注意,它虽然区分大小写。
注意:安装它会在某处爆炸zip并将... \ UnxUtils \ usr \ local \ wbin \添加到系统路径env变量中。
我的PowerShell配置文件中有一个名为'which'
的函数function which {
get-command $args[0]| format-list
}
这是输出的样子:
PS C:\Users\fez> which python
Name : python.exe
CommandType : Application
Definition : C:\Python27\python.exe
Extension : .exe
Path : C:\Python27\python.exe
FileVersionInfo : File: C:\Python27\python.exe
InternalName:
OriginalFilename:
FileVersion:
FileDescription:
Product:
ProductVersion:
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language:
如果你能找到一个免费的Pascal编译器,你可以编译它。至少它起作用并显示必要的算法。
program Whence (input, output);
Uses Dos, my_funk;
Const program_version = '1.00';
program_date = '17 March 1994';
VAR path_str : string;
command_name : NameStr;
command_extension : ExtStr;
command_directory : DirStr;
search_dir : DirStr;
result : DirStr;
procedure Check_for (file_name : string);
{ Check existence of the passed parameter. If exists, then state so }
{ and exit. }
begin
if Fsearch(file_name, '') <> '' then
begin
WriteLn('DOS command = ', Fexpand(file_name));
Halt(0); { structured ? whaddayamean structured ? }
end;
end;
function Get_next_dir : DirStr;
{ Returns the next directory from the path variable, truncating the }
{ variable every time. Implicit input (but not passed as parameter) }
{ is, therefore, path_str }
var semic_pos : Byte;
begin
semic_pos := Pos(';', path_str);
if (semic_pos = 0) then
begin
Get_next_dir := '';
Exit;
end;
result := Copy(Path_str, 1, (semic_pos - 1)); { return result }
{ Hmm! although *I* never reference a Root drive (my directory tree) }
{ is 1/2 way structured), some network logon software which I run }
{ does (it adds Z:\ to the path). This means that I have to allow }
{ path entries with & without a terminating backslash. I'll delete }
{ anysuch here since I always add one in the main program below. }
if (Copy(result, (Length(result)), 1) = '\') then
Delete(result, Length(result), 1);
path_str := Copy(path_str,(semic_pos + 1),
(length(path_str) - semic_pos));
Get_next_dir := result;
end; { Of function get_next_dir }
begin
{ The following is a kludge which makes the function Get_next_dir easier }
{ to implement. By appending a semi-colon to the end of the path }
{ Get_next_dir doesn't need to handle the special case of the last entry }
{ which normally doesn't have a semic afterwards. It may be a kludge, }
{ but it's a documented kludge (you might even call it a refinement). }
path_str := GetEnv('Path') + ';';
if (paramCount = 0) then
begin
WriteLn('Whence: V', program_version, ' from ', program_date);
Writeln;
WriteLn('Usage: WHENCE command[.extension]');
WriteLn;
WriteLn('Whence is a ''find file''type utility witha difference');
Writeln('There are are already more than enough of those :-)');
Write ('Use Whence when you''re not sure where a command which you ');
WriteLn('want to invoke');
WriteLn('actually resides.');
Write ('If you intend to invoke the command with an extension e.g ');
Writeln('"my_cmd.exe param"');
Write ('then invoke Whence with the same extension e.g ');
WriteLn('"Whence my_cmd.exe"');
Write ('otherwise a simple "Whence my_cmd" will suffice; Whence will ');
Write ('then search the current directory and each directory in the ');
Write ('for My_cmd.com, then My_cmd.exe and lastly for my_cmd.bat, ');
Write ('just as DOS does');
Halt(0);
end;
Fsplit(paramStr(1), command_directory, command_name, command_extension);
if (command_directory <> '') then
begin
WriteLn('directory detected *', command_directory, '*');
Halt(0);
end;
if (command_extension <> '') then
begin
path_str := Fsearch(paramstr(1), ''); { Current directory }
if (path_str <> '') then WriteLn('Dos command = "', Fexpand(path_str), '"')
else
begin
path_str := Fsearch(paramstr(1), GetEnv('path'));
if (path_str <> '') then WriteLn('Dos command = "', Fexpand(path_str), '"')
else Writeln('command not found in path.');
end;
end
else
begin
{ O.K, the way it works, DOS looks for a command firstly in the current }
{ directory, then in each directory in the Path. If no extension is }
{ given and several commands of the same name exist, then .COM has }
{ priority over .EXE, has priority over .BAT }
Check_for(paramstr(1) + '.com'); { won't return if file is found }
Check_for(paramstr(1) + '.exe');
Check_for(paramstr(1) + '.bat');
{ Not in current directory, search through path ... }
search_dir := Get_next_dir;
while (search_dir <> '') do
begin
Check_for(search_dir + '\' + paramstr(1) + '.com');
Check_for(search_dir + '\' + paramstr(1) + '.exe');
Check_for(search_dir + '\' + paramstr(1) + '.bat');
search_dir := Get_next_dir;
end;
WriteLn('DOS command not found: ', paramstr(1));
end;
end.
我在Windows上找到的最好的版本是Joseph Newcomer的<!>“whereis <!>”;实用程序,可从他的网站获得(来源)。
关于<!>的发展的文章; whereis <!> quot;值得一读。
我在互联网上找到的 Unix 的 Win32 移植版本都不是令人满意的,因为它们都有以下一个或多个缺点:
- 不支持 Windows PATHEXT 变量。(它定义了在扫描路径之前隐式添加到每个命令的扩展列表以及顺序。)(我使用了很多 tcl 脚本,并且没有公开可用的工具可以找到它们。)
- 不支持 cmd.exe 代码页,这使得它们无法正确显示包含非 ascii 字符的路径。(我对此非常敏感,我的名字中带有 ç :-))
- 不支持 cmd.exe 和 PowerShell 命令行中的不同搜索规则。(没有公开可用的工具可以在 PowerShell 窗口中找到 .ps1 脚本,但不能在 cmd 窗口中找到!)
所以我最终写了自己的,它正确支持上述所有内容。
此批处理文件使用CMD变量处理来查找将在路径中执行的命令。注意:当前目录总是在路径之前完成,并且根据使用的API调用,在路径之前/之后搜索其他位置。
@echo off
echo.
echo PathFind - Finds the first file in in a path
echo ======== = ===== === ===== ==== == == = ====
echo.
echo Searching for %1 in %path%
echo.
set a=%~$PATH:1
If "%a%"=="" (Echo %1 not found) else (echo %1 found at %a%)
请参阅set /?
寻求帮助。
您可以先从 下载Git 安装Git,然后然后打开Git Bash并输入:
which app-name
我正在使用GOW(Windows上的GNU),它是Cygwin的简易版本。你可以从GitHub 这里获取它。
GOW(Windows上的GNU)是Cygwin的轻量级替代品。它用 一个方便的Windows安装程序,安装大约130个极端 编译为本机win32的有用的开源UNIX应用程序 二进制文件。它的设计尽可能小,大约10 MB,如 与Cygwin相反,Cygwin可以运行超过100 MB,具体取决于 选项。 - 关于描述(Brent R. Matzelle)
GOW中包含的命令列表的屏幕截图:
我创建了类似于Ned Batchelder的工具:
虽然我的工具主要用于搜索各种dll版本,但它显示更多信息(日期,大小,版本),但它不使用PATHEXT(我希望尽快更新我的工具)。
对于Windows <!> nbsp; XP用户(没有内置where
命令),我写了一个<!> quot;其中就像<!> quot;命令为名为whichr
的rubygem。
要安装它,请安装Ruby。
然后
gem install whichr
像以下一样运行:
<!>:ÇGT;哪个cmd_here
which
是TCC系列命令处理器的内置命令。 我使用了npm的which
模块已经有一段时间了,而且效果很好: https://www.npmjs.com/package/which
这是一个很好的多平台替代方案。
现在我切换到Git附带的/usr/bin
。只需在Git中添加C:\Program Files\Git\usr\bin\which.exe
路径,该路径通常位于<=>。 <=>二进制文件位于<=>。它更快,也可以按预期工作。
试试这个
set a=%~$dir:1
If "%for%"=="" (Echo %1 not found) else (echo %1 found at %a%)