Karena saya kadang-kadang memiliki masalah jalur, di mana salah satu skrip cmd saya disembunyikan (dibayangi) oleh program lain (sebelumnya di jalur), saya ingin dapat menemukan path lengkap ke program di baris perintah Windows, mengingat hanya namanya saja.
Apakah ada yang setara dengan perintah UNIX 'yang'?
Pada UNIX, which command
mencetak path lengkap dari perintah yang diberikan untuk dengan mudah menemukan dan memperbaiki masalah bayangan ini.
Windows Server 2003 dan yang lebih baru (mis. Apa pun setelah Windows XP 32 bit) menyediakan program where.exe
yang melakukan beberapa hal yang which
lakukan, meskipun cocok dengan semua jenis file, bukan hanya perintah yang dapat dieksekusi. (Ini tidak cocok dengan perintah Shell bawaan seperti cd
.) Ia bahkan akan menerima wildcard, jadi where nt*
menemukan semua file di %PATH%
Anda dan direktori saat ini yang namanya dimulai dengan nt
.
Coba where /?
untuk bantuan.
Perhatikan bahwa Windows PowerShell mendefinisikan where
sebagai alias untuk Where-Object
cmdlet , jadi jika Anda ingin where.exe
, Anda perlu mengetikkan nama lengkap alih-alih menghilangkan ekstensi .exe
.
Sementara versi Windows yang lebih baru memiliki perintah where
, Anda juga dapat melakukan ini dengan Windows XP dengan menggunakan pengubah variabel lingkungan, sebagai berikut:
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
Anda tidak memerlukan alat tambahan apa pun dan tidak terbatas pada PATH
karena Anda dapat mengganti variabel lingkungan apa pun (dalam format jalur, tentu saja) yang ingin Anda gunakan.
Dan, jika Anda menginginkannya yang dapat menangani semua ekstensi di PATHEXT (seperti halnya Windows itu sendiri), yang ini berguna:
@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
Ini sebenarnya mengembalikan semua kemungkinan tetapi Anda bisa men-tweak dengan mudah untuk aturan pencarian tertentu.
Di bawah PowerShell, Get-Command
akan menemukan executable di mana saja di $Env:PATH
.
Get-Command eventvwr
CommandType Name Definition
----------- ---- ----------
Application eventvwr.exe c:\windows\system32\eventvwr.exe
Application eventvwr.msc c:\windows\system32\eventvwr.msc
Ia juga menemukan cmdlet PowerShell, fungsi, alias, file dengan ekstensi custom executable melalui $Env:PATHEXT
, dll. Didefinisikan untuk Shell saat ini (sangat mirip dengan Bash's type -a foo
) - membuatnya lebih baik daripada alat lain seperti where.exe
, which.exe
, dll. tidak mengetahui perintah PowerShell ini.
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
...
Untuk menemukan executable non-windows lainnya (python, Ruby, Perl, dll), ekstensi file untuk executable tersebut perlu ditambahkan ke variabel lingkungan PATHEXT
(default ke .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
) untuk mengidentifikasi file dengan ekstensi ini di PATH
sebagai executable. Karena Get-Command
juga menghargai variabel ini, ia dapat diperluas ke daftar executable khusus. misalnya.
$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
Anda dapat dengan cepat mengatur alias dengan sal which gcm
(bentuk pendek set-alias which get-command
).
Informasi dan contoh lebih lanjut dapat ditemukan di bawah bantuan online untuk Get-Command
.
Di Windows PowerShell:
set-alias which where.exe
Jika Anda telah menginstal PowerShell (yang saya rekomendasikan), Anda dapat menggunakan perintah berikut sebagai setara kasar (gantilah nama program untuk nama yang dapat dieksekusi):
($Env:Path).Split(";") | Get-ChildItem -filter programName*
Lebih banyak di sini:My Manwich! PowerShell Yang
The GnuWin32 tools memiliki which
, bersama dengan sejumlah besar alat Unix lainnya.
Pada Windows CMD which
panggilan where
:
$ where php
C:\Program Files\PHP\php.exe
Cygwin adalah solusi. Jika Anda tidak keberatan menggunakan solusi pihak ketiga, maka Cygwin adalah caranya.
Cygwin memberi Anda kenyamanan * nix di lingkungan Windows (dan Anda dapat menggunakannya di Shell perintah Windows Anda, atau menggunakan * * nix Shell pilihan Anda). Ini memberi Anda seluruh Host perintah * nix (seperti which
) untuk Windows, dan Anda bisa memasukkan direktori itu ke PATH
Anda.
Di PowerShell, itu adalah gcm
, yang memberikan informasi yang diformat tentang perintah lain. Jika Anda ingin mengambil hanya path ke executable, gunakan .Source
.
Misalnya: gcm git
atau (gcm git).Source
Informasi:
gcm
adalah alias dari Get-Command
cmdlet .Set-Alias which gcm
dan menggunakannya seperti: (which git).Source
.Saya memiliki fungsi di profil PowerShell saya bernama 'which'
function which {
get-command $args[0]| format-list
}
Seperti apa outputnya:
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:
Dapatkan unxutils dari sini: http://sourceforge.net/projects/unxutils/
emas pada platform windows, menempatkan semua utilitas Nice unix pada windows DOS standar. Sudah menggunakannya selama bertahun-tahun.
Ini memiliki 'yang' disertakan. Perhatikan bahwa case case sensitif.
NB: untuk menginstalnya meledak Zip di suatu tempat dan tambahkan ...\UnxUtils\usr\local\wbin\ke jalur sistem Anda env variabel.
Jika Anda dapat menemukan kompilator Pascal gratis, Anda dapat mengkompilasi ini. Setidaknya itu berfungsi dan menunjukkan algoritma yang diperlukan.
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.
Tidak dalam persediaan Windows tetapi disediakan oleh Layanan untuk Unix dan ada beberapa skrip batch sederhana yang beredar yang menghasilkan hal yang sama seperti ini ini .
Versi terbaik dari ini saya temukan di Windows adalah utilitas "whereis" Joseph Newcomer, yang tersedia (dengan sumber) dari situsnya .
Artikel tentang pengembangan "whereis" layak dibaca.
File batch ini menggunakan penanganan variabel CMD untuk menemukan perintah yang akan dieksekusi di path. Catatan: bahwa direktori saat ini selalu dilakukan sebelum path) dan tergantung pada panggilan API mana yang digunakan, lokasi lain dicari sebelum/setelah path.
@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%)
Lihat set /?
untuk bantuan.
Tidak satu pun dari port Win32 Unix yang dapat saya temukan di Internet memuaskan, karena mereka semua memiliki satu atau lebih dari kekurangan ini:
Jadi saya akhirnya menulis sendiri, yang mendukung semua hal di atas dengan benar.
Tersedia di sana: http://jf.larvoire.free.fr/progs/which.exe
Saya menggunakan GOW (GNU pada Windows) yang merupakan versi ringan dari Cygwin. Anda dapat mengambilnya dari GitHub di sini .
GOW (GNU pada Windows) adalah alternatif ringan untuk Cygwin. Ia menggunakan penginstal Windows yang nyaman yang menginstal sekitar 130 aplikasi UNIX open source yang sangat berguna yang dikompilasi sebagai binari win32 asli. Ini dirancang untuk menjadi sekecil mungkin, sekitar 10 MB, berbeda dengan Cygwin yang dapat berjalan lebih dari 100 MB tergantung pada opsi. - Tentang Keterangan (Brent R. Matzelle)
Tangkapan layar daftar perintah yang termasuk dalam GOW:
Saya telah membuat alat yang mirip dengan Ned Batchelder:
Mencari file .dll dan .exe di PATH
Sementara alat saya terutama untuk mencari berbagai versi dll itu menunjukkan lebih banyak info (tanggal, ukuran, versi) tetapi tidak menggunakan PATHEXT (Saya berharap untuk memperbarui alat saya segera).
Anda dapat menginstal Git dariMengunduh Git, dan kemudian buka Git Bash dan ketik:
which app-name
Untuk Anda pengguna Windows XP (yang tidak memiliki perintah bawaan where
), saya telah menulis perintah "where like" sebagai rubygem yang disebut whichr
.
Untuk menginstalnya, instal Ruby.
Kemudian
gem install whichr
Jalankan seperti:
C:> whichr cmd_di sini
TCC dan TCC/LE dari JPSoft adalah pengganti CMD.EXE yang menambah fungsionalitas yang signifikan. Relevan dengan pertanyaan OP, which
adalah perintah bawaan untuk prosesor perintah keluarga TCC.
Saya telah menggunakan modul which
dari npm selama beberapa waktu, dan ia bekerja dengan sangat baik: https://www.npmjs.com/package/which Ini adalah alternatif multi platform yang hebat.
Sekarang saya beralih ke which
yang datang bersama Git. Tambahkan saja ke path Anda path /usr/bin
dari Git, yang biasanya di C:\Program Files\Git\usr\bin\which.exe
. Biner which
akan berada di C:\Program Files\Git\usr\bin\which.exe
. Lebih cepat dan juga berfungsi seperti yang diharapkan.
coba ini
set a=%~$dir:1
If "%for%"=="" (Echo %1 not found) else (echo %1 found at %a%)