Windowsコマンドラインに「which」に相当するものはありますか?
-
08-07-2019 - |
質問
パスの問題が時々あるので、自分の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 <を省略する代わりにフルネームを入力する必要があります。 / code>拡張機能。
他のヒント
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
に限定されません。
そして、もし(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コマンドレット、関数、エイリアス、 $ Env:PATHEXT
を介したカスタム実行可能ファイル拡張子を持つファイルなども、現在のシェルに対して定義されています(Bashの type -a foo
)-これらのPowerShellコマンドを認識しない 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
...
カスタム実行可能ファイルの検索
他の非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 <のオンラインヘルプにあります。 / code>
。
Windows PowerShellの場合:
set-alias which where.exe
PowerShellがインストールされている場合(推奨)、次のコマンドを大まかな同等のものとして使用できます(実行可能ファイルの名前をprogramNameに置き換えます)。
($Env:Path).Split(";") | Get-ChildItem -filter programName*
詳細はこちら: マイマンウィッチ! PowerShell which
GnuWin32 ツールには、 which
と、他の多くのUnixがあります。ツール。
Windows CMD which
で where
を呼び出す場合:
$ where php
C:\Program Files\PHP\php.exe
Cygwin は解決策です。サードパーティのソリューションを使用しても構わない場合は、Cygwinが最適です。
Cygwinは、Windows環境で* nixの快適さを提供します(また、Windowsコマンドシェルで使用するか、任意の* nixシェルを使用できます)。 Windows用の* nixコマンド( which
など)のホスト全体を提供し、そのディレクトリを PATH
に含めることができます。
PowerShellでは、 gcm
であり、他のコマンドに関するフォーマットされた情報を提供します。実行可能ファイルへのパスのみを取得する場合は、 .Source
を使用します。
たとえば: gcm git
または(gcm git).Source
注意点:
- Windows XPで利用可能。
- PowerShell 1.0以降で使用可能。
-
gcm
はGet-Command のエイリアスですcode>コマンドレット
。 - パラメータなしで、ホストシェルが提供する使用可能なすべてのコマンドをリストします。
-
Set-Alias which gcm
でカスタムエイリアスを作成し、(which git).Source
のように使用できます。 - 公式ドキュメント: https://technet.microsoft.com/en-us /library/ee176842.aspx
ここからunxutilsを入手してください: http://sourceforge.net/projects/unxutils/
Windowsプラットフォームのgoldは、すべての素敵なUNIXユーティリティを標準のWindows DOSに置きます。何年も使用しています。
「which」が含まれています。ただし、大文字と小文字が区別されることに注意してください。
NB:インストールするには、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」です。 彼のサイトから(ソースと共に)利用可能なユーティリティ。
&quot; whereis&quot;の開発に関する記事読む価値があります。
インターネット上で見つけることができるUnixのWin32ポートはどれも満足のいくものではありません。それらはすべて、次の1つ以上の欠点があるためです。
- 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
CygwinのライトバージョンであるGOW(WindowsではGNU)を使用しています。 GitHub こちらから取得できます。
GOW(Windows上のGNU)は、Cygwinの軽量の代替です。それは使用しています 約130を非常にインストールする便利なWindowsインストーラー ネイティブwin32としてコンパイルされた便利なオープンソースUNIXアプリケーション バイナリ。可能な限り小さく、約10 MBになるように設計されています。 Cygwinとは異なり、 オプション。 -説明について(Brent R. Matzelle)
GOWに含まれるコマンドのリストのスクリーンショット:
Ned Batchelderに似たツールを作成しました:
私のツールは主にさまざまなdllバージョンの検索に使用されますが、詳細情報(日付、サイズ、バージョン)を表示しますが、PATHEXTは使用しません(すぐにツールを更新したいと考えています)。
Windows&nbsp; XPユーザー( where
コマンドが組み込まれていない)のために、「where like」を書きました。 whichr
と呼ばれるrubygemとしてのコマンド。
それをインストールするには、Rubyをインストールします。
その後
gem install whichr
次のように実行します:
C:&gt; whichr cmd_here
JPSoftのTCCおよびTCC / LEは、重要な機能を追加するCMD.EXEの代替品です。 OPの質問に関連して、 which
はTCCファミリコマンドプロセッサの組み込みコマンドです。
かなり前からnpmの which
モジュールを使用しましたが、非常にうまく機能します: https://www.npmjs.com/package/which
これは、マルチプラットフォームの優れた代替手段です。
今、Gitに付属の which
に切り替えました。通常は C:\ Program Files \ Git \ usr \ bin \ which.exe
にあるGitの / usr / bin
パスをパスに追加します。 which
バイナリは C:\ Program Files \ Git \ usr \ bin \ which.exe
にあります。より高速で、期待どおりに動作します。
これを試してください
set a=%~$dir:1
If "%for%"=="" (Echo %1 not found) else (echo %1 found at %a%)