由于我有时遇到路径问题,其中一个我自己的cmd脚本被另一个程序隐藏(阴影)(在路径的前面),我希望能够在Windows命令行上找到程序的完整路径,给定只是它的名字.
有没有相当于UNIX命令'哪个'?
在UNIX上,which command
打印给定命令的完整路径以轻松查找和修复这些阴影问题.
Windows Server 2003及更高版本(即Windows XP 32位之后的任何内容)提供了where.exe
执行某些操作的程序which
,尽管它匹配所有类型的文件,而不仅仅是可执行命令.(它与内置的shell命令不匹配cd
.)它甚至会接受通配符,因此where nt*
查找%PATH%
名称以及以当前目录开头的所有文件nt
.
尝试where /?
寻求帮助.
需要注意的是Windows PowerShell中定义where
为一个别名的Where-Object
cmdlet的,所以如果你想where.exe
,你需要输入完整的名称,而不是省略的.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 ^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命令,函数,别名文件通过自定义的可执行文件的扩展$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
可执行文件中标识具有这些扩展名的文件.由于Get-Command
还表彰这个变量,它可以扩展到列出自定义可执行文件.例如
$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
您可以使用sal which gcm
(简短形式set-alias which get-command
)快速设置别名.
可以在在线帮助下找到更多信息和示例Get-Command
.
在Windows PowerShell中:
set-alias which where.exe
如果安装了PowerShell(我建议使用),则可以使用以下命令作为粗略等效命令(替换programName作为可执行文件的名称):
($Env:Path).Split(";") | Get-ChildItem -filter programName*
更多的是: 我的曼彻斯特!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).它which
为Windows 提供了大量的*nix命令(例如),你可以在你的目录中包含该目录PATH
.
在PowerShell中,它gcm
提供了有关其他命令的格式化信息.如果要仅检索可执行文件的路径,请使用.Source
.
例如:gcm git
或(gcm git).Source
小知识:
适用于Windows XP.
自PowerShell 1.0起可用.
gcm
是Get-Command
cmdlet的别名.
没有任何参数,它列出了主机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,但它是由提供服务的UNIX并且有完成同样的事情,例如这几个简单的批处理脚本漂浮这一个.
我在Windows上找到的这个最好的版本是Joseph Newcomer的"whereis"实用程序,可从他的网站获得(带源代码).
关于"whereis"发展的文章值得一读.
我在互联网上找到的Unix的Win32端口都不是satistactory,因为它们都有一个或多个这些缺点:
不支持Windows PATHEXT变量.(它定义了在扫描路径之前隐含地添加到每个命令的扩展名列表,以及顺序.)(我使用了很多tcl脚本,并且没有公开可用的工具可以找到它们.)
不支持cmd.exe代码页,这使得它们错误地显示非ascii字符的路径.(我对此很敏感,用我的名字ç:-))
不支持cmd.exe和PowerShell命令行中的不同搜索规则.(没有公开可用的工具会在PowerShell窗口中找到.ps1脚本,但不会在cmd窗口中找到!)
所以我最终写了自己的,这正确地支持了以上所有内容.
可在那里找到:http: //jf.larvoire.free.fr/progs/which.exe
此批处理文件使用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 /?
寻求帮助.
你可以先从Downloading Git安装Git ,然后打开Git Bash并输入:
which app-name
我正在使用GOW(Windows上的GNU),它是Cygwin的简易版本.你可以在这里从GitHub获取它.
GOW(Windows上的GNU)是Cygwin的轻量级替代品.它使用方便的Windows安装程序安装大约130个非常有用的开源UNIX应用程序,这些应用程序编译为本机win32二进制文件.它设计得尽可能小,大约10 MB,而Cygwin则可以运行超过100 MB,具体取决于选项.- 关于描述(Brent R. Matzelle)
GOW中包含的命令列表的屏幕截图:
我创建了类似于Ned Batchelder的工具:
在PATH中搜索.dll和.exe文件
虽然我的工具主要用于搜索各种dll版本,但它显示更多信息(日期,大小,版本),但它不使用PATHEXT(我希望尽快更新我的工具).