JSI Tip 8204. How can I return a list of files that contain a specified string?


I have scripted FindIt.bat to return a list of files that contain a specified string.

The syntax for using FindIt.bat is:

\[call\] FindIt StartPath Mask "String"

where:

StartPath     

is the drive or folder you want to search. All sub-folders of StartPath are also searched.

Mask

is the file mask. An *.* will search all files in StartPath, while a mask of *.txt will only search files with a .txt extension.

"String" is the string you are searching for. It MUST be quoted.

The fully qualified file name output is display on the console, but you can pipe it to a file using:

\[call\] FindIt StartPath Mask "String">FileName

You can process the files returned using:

for /f "Tokens=*" %%f in ('FindIt StartPath Mask "String"') do (
 set file=%%f
 call :process
)

             OR

for /f "Tokens=*" %%f in ('FindIt StartPath Mask "String"') do (
 call :process "%%f"
)
FindIt.bat contains:
@echo off
if \{%3\}==\{\} @echo Syntax: FindIt Path mask "String"&goto :EOF
if not exist %1 @echo Path Not Found: FindIt %1 %2 %3&goto :EOF
setlocal
set folder=%1
set mask=%2
set string=%3
if exist "%TEMP%\findit.log" del /q "%TEMP%\findit.log"
call :quiet>>nul 2>>&1
if exist "%TEMP%\findit.log" type "%TEMP%\findit.log"
if exist "%TEMP%\findit.log" del /q "%TEMP%\findit.log"
endlocal
goto :EOF
:quiet
for /R %folder% %%a in (%mask%) do (
 for /f "Tokens=*" %%b in ('findstr /i /L /M /c:%string% "%%a"') do (
 @echo %%a>>"%TEMP%\findit.log"
 )
)



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish