自分の cmd スクリプトの 1 つが別のプログラム (パスの前) によって隠されている (シャドウされている) というパスの問題が時々あるので、与えられた Windows コマンド ラインでプログラムへのフル パスを見つけられるようにしたいと考えています。その名前だけです。
UNIX コマンドの「which」に相当するものはありますか?
UNIX ではwhich command
、特定のコマンドのフル パスを出力して、これらのシャドウイングの問題を簡単に見つけて修復します。
自分の cmd スクリプトの 1 つが別のプログラム (パスの前) によって隠されている (シャドウされている) というパスの問題が時々あるので、与えられた Windows コマンド ラインでプログラムへのフル パスを見つけられるようにしたいと考えています。その名前だけです。
UNIX コマンドの「which」に相当するものはありますか?
UNIX ではwhich command
、特定のコマンドのフル パスを出力して、これらのシャドウイングの問題を簡単に見つけて修復します。
Windows Server 2003以降(つまり、Windows XP 32ビット以降)は、実行可能コマンドだけでなく、すべての種類のファイルに一致しますが、where.exe
一部の機能を実行するプログラムを提供します。which
(のような組み込みのシェルコマンドとは一致しませんcd
。)ワイルドカードも受け入れるため、名前が。で始まる現在のディレクトリwhere nt*
内のすべてのファイルを検索します。%PATH%
nt
where /?
助けを求めてください。
Windows PowerShellはコマンドレットwhere
のエイリアスとして定義されているため、Where-Object
必要に応じて、拡張子where.exe
を省略するのではなく、フルネームを入力する必要があります。.exe
または、エイリアスを設定することもできます。
Set-Alias which where.exe
更新:(Get-Command
エイリアス:)を使用gcm
することをお勧めします。これは、PSにネイティブであり、すべてのコマンドタイプ(エイリアス、コマンドレット、実行可能ファイル、および関数)を取得するためです。例:
gcm notepad*
それ以降のバージョンのWindowsにはwhere
コマンドがありますが、次のように、環境変数修飾子を使用してWindowsXPでこれを行うこともできます。
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
。使用したい環境変数(もちろんパス形式)を置き換えることができるため、これに限定されません。
そして、(Windows自体が行うように)PATHEXTのすべての拡張機能を処理できるものが必要な場合は、これでうまくいきます。
@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 ではエイリアスをwhich
定義できるため、そのように定義できます。
$ sal which gcm # short form of `Set-Alias which Get-Command`
$ which foo
...
PowerShellコマンドは単なる実行可能ファイル ( .exe
、.ps1
など) ではありません。また、コマンドレット、関数、エイリアス、カスタム実行可能サフィックスを に設定することもできます$Env:PATHEXT
。Get-Command
は、これらすべてのコマンドを検索して一覧表示できます (Bash の に非常に似ていますtype -a foo
)。これだけでも、通常は実行可能ファイルのみの検索に限定される 、 などwhere.exe
よりも優れています。which.exe
$ 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
...
実行可能ファイルが実行可能 ( +x
) ビットが設定されたファイルである UNIX とは異なり、Windows 上の実行可能ファイルは、$PATH
env で指定されたディレクトリのいずれかに存在するファイルです。ファイル名サフィックスが$PATHEXT
env で指定されている変数。変数 (デフォルトは.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
)。
またGet-Command
、このenvを尊重します。変数で、カスタム実行可能ファイルをリストするように拡張できます。例えば
$ $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
Get-Command
その他のオプションと例については、を参照してください。
Windows PowerShell の場合:
set-alias which where.exe
PowerShellがインストールされている場合(これをお勧めします)、次のコマンドを大まかに同等のものとして使用できます(実行可能ファイルの名前をprogramNameに置き換えてください)。
($Env:Path).Split(";") | Get-ChildItem -filter programName*
詳細はこちら: 私のマンウイッチ!PowerShell
GnuWin32ツールには、その他のwhich
多数の Unix ツールと共に があります。
Windows CMDwhich
呼び出しの場合where
:
$ where php
C:\Program Files\PHP\php.exe
Cygwinはソリューションです。サードパーティのソリューションを使用してもかまわない場合は、Cygwin が最適です。
Cygwin は Windows 環境で快適な *nix を提供します (また、Windows コマンド シェルで使用することも、任意の *nix シェルを使用することもできます)。which
Windows用の* nixコマンド(のような)のホスト全体を提供し、そのディレクトリをPATH
.
PowerShell では、gcm
他のコマンドに関する書式設定された情報を提供する です。実行可能ファイルへのパスのみを取得する場合は、 を使用します.Source
。
例:gcm git
または(gcm git).Source
ちょっとした情報:
gcm
Get-Command
コマンドレットのエイリアスです。Set-Alias which gcm
ように使用できます(which git).Source
。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:
ここから unxutils を入手してください: http://sourceforge.net/projects/unxutils/
Windows プラットフォームでの gold は、すべての優れた UNIX ユーティリティを標準の Windows DOS に配置します。何年も使用しています。
「which」が含まれています。ただし、大文字と小文字が区別されることに注意してください。
注意: インストールするには、zip を展開して、システム パスの環境変数に ...\UnxUtils\usr\local\wbin\ を追加します。
無料の 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 の在庫はありませんが、Services for Unixによって提供されており、これと同じことを実現する簡単なバッチ スクリプトがいくつか出回っています。
私がWindowsで見つけたこれの最高のバージョンは、Joseph Newcomerの「whereis」ユーティリティです。これは、彼のサイトから(ソースを使用して)入手できます。
「whereis」の開発に関する記事は読む価値があります。
Ned Batchelder に似たツールを作成しました。
私のツールは主にさまざまな dll バージョンを検索するためのものですが、より多くの情報 (日付、サイズ、バージョン) を表示しますが、PATHEXT は使用しません (ツールをすぐに更新したいと考えています)。
Windows XP ユーザー (where
コマンドが組み込まれていない) のために、「where like」コマンドを という rubygem として作成しましたwhichr
。
インストールするには、Ruby をインストールします。
それで
gem install whichr
次のように実行します。
C:> whichr cmd_here
私はwhich
かなり長い間 npm のモジュールを使用してきましたが、非常にうまく機能します: https://www.npmjs.com/package/
これは優れたマルチプラットフォームの代替手段です。
which
ここで、Git に付属の に切り替えました。Git からのパスをパスに追加するだけ/usr/bin
で、通常はC:\Program Files\Git\usr\bin\which.exe
. which
バイナリは になりますC:\Program Files\Git\usr\bin\which.exe
。より高速で、期待どおりに動作します。
Windows 用にコンパイルされた UNIX コマンドはすべて、which
次の GitHub リポジトリからダウンロードできます: https://github.com/George-Ogden/UNIX