Home > Mobile >  Windows cmd: piping the dir list output into find (or findstr) is not working
Windows cmd: piping the dir list output into find (or findstr) is not working

Time:08-24

I need to do some very big Windows searches for some specific searchterms in th contents of all the files in a folder and all sub-folders. The GUI search facility is not finding all my tests, so I would like to try to use find via the cmd.

I can list all filenames in raw data format using:-

dir /S /B

I can successfully search for the searchterm in thecontents of all files in a single folder using :-

find "Searchterm" *.*

But there are thousands of recursive sub-folders, so when I pipe the output from the dir listing to the find (and exclude the filename parameter):

dir /S /B | find "Searchterm"

I am getting no results.

Furthermore, I have also successfully sent all the dir /B /S filenames to a text file:-

dir /S /B >> filenames.txt

and using type to pipe the contents of each file from the list to the find :-

type filenames.txt | find "Searchstring"

This does not work either. What am I missing? Microsoft's documentation suggests exactly the same format in https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find as I am trying.

CodePudding user response:

The solution to your question(s) should be clear by reading the output from %SystemRoot%\System32\findstr.exe /? ENTERed in a Command Prompt window.

I'd advise that you use the /L, literal, option for your initial code.

Direct results example:

%SystemRoot%\System32\findstr.exe /I /L /P /S "Searchstring" *

If you first send the filenames to a text file, e.g. Dir /B /S /A:-D 2>NUL 1>"filenames.txt", you could use the following idea:

%SystemRoot%\System32\findstr.exe /F:"filenames.txt" /I /L /P "Searchstring"

Just be aware, in this case, that unless you include a path outside of the target tree when initially creating filenames.txt, it will include itself in its own content. That means your FindStr command will also pick up any matches in that file too.

  • Related