Saya tahu ini sudah dijawab lama, namun ini skrip saya untuk mensimulasikan fungsi loc yang digunakan di unix / linux
Simpan ini sebagai loc.bat dan letakkan di System32 ATAU jalankan dari direktori tempat penyimpanannya. Mengambil satu parameter seperti ini "cmd> cari javac.exe"
Switch temukan / c (sebagai parameter ke-2 akan menghitung jumlah instance) dan cari /? akan menampilkan teks bantuan
@echo off
if [%1]==[] goto :usage
if [%1] == [/?] (
:usage
echo Usage:
echo locate "filename" { optional } /c { shows counter }
echo note: results are exported to C:\temp\result.txt
goto :EOF
) else (
setlocal EnableDelayedExpansion
dir /a /b /s C:\ | findstr /I "%1" > C:\temp\result.txt
type C:\temp\result.txt | more /S
set counter=0
if [%2] == [/c] (
for /F %%i in ('type C:\temp\result.txt') do (
set /a counter=!counter!+1
)
echo Total Matches: !counter!
)
)
endlocal > nul
goto :EOF
EDIT: (Script Diperbarui, lebih terorganisir dan dapat menangani berbagai situasi yang lebih luas, seperti memungkinkan istilah pencarian apa pun bukan hanya nama file)
@echo off
::initialize local varaibles to default values
setlocal
set file=
set directory=
set toppath=top
::some switch validation ( from command line )
if [%1]==[] goto :Usage
if [%1]==[/?] goto :Usage
if [%1]==[/help] goto :Usage
::handle switches with if structures
if [%2]==[/c] (
set count=yes
) ELSE (
if [%2]==[/t] ( if [%3]==[] (goto :Usage) else (set toppath=%3))
if [%4]==[/c] (
set count=yes
) ELSE (
set count=
)
)
set file=%1
::Directory Validation ( Goto jumps possible, along with raptors )
if [%toppath%] == [] (
IF NOT EXIST %toppath% goto :FolderInvalid
) else (
if [%toppath%] neq [top] set directory=%toppath%
)
set toppath=
setlocal EnableDelayedExpansion
::Run Bulk of the Script
dir /a /b /s %directory% | findstr /I "%file%" > C:\temp\result.txt
type C:\temp\result.txt | more /S
set counter=0
if [%count%]==[yes] (
for /F %%i in ('type C:\temp\result.txt') do (
set /a counter=!counter!+1
)
echo Total Matches: !counter!
)
goto :End
:Usage
echo locate ^[file^] {term^/file to look for} ^| ^[^/t^] {dir^/subdirs to search} ^| ^[^/c^] {show counter}
echo.
echo notes to user: 1. Results are exported to C:\temp\result.txt
echo 2. Default search dir is the current unless specified by ^/t switch
echo 3. For best results write switches in order given ! ( or beware of raptors )
goto :End
:FolderInvalid
echo Folder Invalid
:End
endlocal > nul