1

In my current script, i am using findstr (Windows) as follows:

findstr /s "string" C:\*.*

but this is extremely slow. What is the fastest way to do this in Windows without using any additional software (e.g. python, c#, etc...). Also, the files in the directories are constantly changing, so i'm unable to index the files and perform a search on the index.

The results need the full path and filename with the string match. The full lines where the string matches need to be returned. Only text based files need to be searched (e.g. xml, txt, etc...)

1
  • Post has been updated. Commented Aug 24, 2015 at 1:25

1 Answer 1

0

A possible batch file for this task would be:

@echo off
cd /D C:\
del "%USERPROFILE%\SearchResults.txt" 2>nul
%SystemRoot%\system32\findstr.exe /I /S /C:"string" *.htm *.html *.txt *.xml >"%USERPROFILE%\SearchResults.tmp"
ren "%USERPROFILE%\SearchResults.tmp" "SearchResults.txt"

First the current directory is changed to root of drive C:.

Next a perhaps existing search results file from a previous run on desktop of current user is deleted.

Then findstr is executed to search for the string case-insensitive in the files specified with wildcards on the command line. More file extensions can be appended. There is no file extension for "all text files".

findstr prints for each found occurrence the name of the file and the found line to stdout which is redirected into a text file with file extension tmp to desktop of current user. It is not advisable to give the results file a file extension being specified also on command line of findstr.

Finally the created results file is renamed to change file extension from tmp to txt.

But searching for a string in thousands of files with writing all found lines with name of file into a results file needs time. On second run the tasks finishes already faster because Windows will have many files already loaded in cache and does not access a second time the storage media for those files not changed in the meantime.

BTW: A case-sensitive search done with removing /I is much faster than a case-insensitive search.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.