I know this one was answered a long time ago, however this is my script to simulate the locate function used in unix/linux
Save this as locate.bat and place in System32 OR run from directory it's stored in. Takes one parameter like this "cmd> locate javac.exe"
The locate /c switch (as a 2nd parameter will count the number of instances )
and locate /? will display the help text
@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: (Updated Script, more organized and can handle a wider variety of situations, such as allowing for any search term not just a filename)
@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
1
@JohnT that location no longer works - the updated locate32 home is https://locate32.cogit.net/
– TrojanName – 2018-10-08T15:55:01.5831
Try the Git-Bash.exe aka MINGW which includes
– MarkHu – 2019-04-04T22:45:56.177locate
andupdatedb
--per https://stackoverflow.com/questions/36841241/can-i-get-the-locate-to-work-on-windows-through-git-bash3
Nothing built-in to the Windows command-line is really similar to locate. I would recommend locate32 ( http://www.locate32.net/ )
– John T – 2011-08-23T19:58:18.867javac
should usually be at the same place... – Tamara Wijsman – 2011-08-23T21:47:37.083