How to use findstr to search multiple files - windows

When I use findstr on a single file, like this:
findstr "import" filename.py
it returns a number of matches. But when I extend it to cover the current folder and sub-folders:
findstr /S "import" .
it doesn't find any matches. What am I doing wrong?

OK i've figured it out. The /S option indicates searching current directory and all sub-directories, but I still need to specify a filename pattern.
This indicates all files:
findstr /S "import" *

Related

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

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://learn.microsoft.com/en-us/windows-server/administration/windows-commands/find as I am trying.
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.

Use Windows findstr to list files not containing a string

From Windows CMD I can use
findstr -m subroutine *.f90
to list all files with suffix .f90 containing "subroutine". To list all .f90 files not containing the string I can do something like
dir /b *.f90 > files.txt
findstr -m subroutine *.f90 > files_with_string.txt
and then write a script to list the lines in files.txt not found in files_with_string.txt. Is there a more elegant way?
There is a /v option in findstr, but that wouldn't help here.
Process each file with a for loop, try to find the string and if it doesn't find it (||), echo the filename:
for %a in (*.f90) do #findstr "subroutine" "%a" >nul || echo %a
(above is command line syntax. For use in a batchfile, use %%a instead of %a (all three occurences))
I needed to search for filenames which contained one specific string ("up"), but did not contain another string ("packages").
However, I was hoping to run it from the command line.
This is actually possible to do, you just have to call findstr twice.
Mine looked like:
dir /B /S up | findstr /I "up" | findstr /I /v "packages"
That means:
search all directories (/S & subdirs)
give me the bare formatting (/B)
and pass it through (| pipe) findstr (/I ignore case) to find ones that have "up" then
pass the results (| pipe) through findstr again but this time ignore all that
contain (/v) "packages"
If you have items like:
c:\test\packages\up
c:\extra\thing\up
c:\extra\thing\packages\up
c:\extra\test\up
c:\extra\test\nothing
The results would be only the ones that contain "up" but do not contain "packages"
c:\extra\thing\up
c:\extra\test\up
Call findstr /v multiple times on result
In other words you can keep passing the result into another findstr with /v to remove the ones that have additional words you don't want.

Find specific text in files

Simple as the title says... I thought I could use these commands because of what the find command says on its help but it is not working for me. Am I missing anything?
dir /s /f *.txt | find "READONLY"
With that I want to find every occurrence of the word "READONLY" in every .txt file.
Edit: While writing this question I looked for the suggestions and the findstr command showed up. But it is not finding anything at all.
I'm not sure what you are trying to do.
The /F option is not valid for the DIR command. Correction - I'm told the /F option is a Windows 8 feature. Ignoring that problem, your command is attempting to look for the string READONLY in the output of the DIR command. That will never happen unless a folder or file name contains the text "READONLY" in the name.
If you want to list files that contain "READONLY" in the file content, then:
findstr /s /m READONLY *.txt
The above is case sensitive. If you want case insensitive, then add the /I option.
findstr /s /m /i READONLY *.txt
If you want to list files that have the READONLY attribute, (you cannot modify them), then
dir /s /ar *.txt
find every occurrence of the word "READONLY" in every .txt file [recursively starting from the current directory]
for /f "tokens=1 delims=" %f in ('dir/s/b *.txt') do findstr "READONLY" %f
Be sure to replace % with %% if you're running within a script.
Try using
grep -R 'READONLY' *.txt

FindStr Usages in Windows

Just trying to search a particular text inside files belonging to a directory(and all its subdirectories).
1) findstr /f:"C:\\test.txt" "TextForSearch" > "out.txt"
gives me the correct answer. test.txt here contains all the files that are to be searched.It is being re-directed to out.txt.
Now, out.txt contains all the matching instances but are not delimited using spaces. How to get a list of all filename(only filenames) with a space or delimiter in the file out.txt?
2) findstr /D:"C:\\" "TextForSearch"
does not search at all. It simply hangs? Where am I going wrong here?
findstr /S /D:"C:\\" "TextForSearch" *
/S recersion Searches for matching files in the current directory and all subdirectories.
* search for all files
For others,
Thanks Ted and Harry.
The solutions are
1) 1) findstr /m /f:"C:\\test.txt" "TextForSearch" > "out.txt"
for searching for a particular strings in files specified in test.txt and redirecting it to a text file named out.txt with matching files in separate lines (Demilited. Thank god.)
findstr /S /D:"C:\\" "TextForSearch" *
checkout Ted's answer!

Windows CMD: List files in dir & subdir WITHOUT given extensions

I'd like to recursively search a directory and find files, which have NOT a certain extension, or precisely, which have NOT a certain set of extensions.
Sketch: find in "dir" all files without "ext1", "ext2", "ext3" and print results to .txt
I tried around several hours with DIR and ATTRIB, but unfortunately without bigger success.
Your consideration is highly regarded! Thanks.
Try this:
dir /b /s /a-d | findstr /vi ".ext1$ .ext2$ .ext3$"
The /a-d switch excludes directories, giving you only files. The findstr parameter lets you search the files for strings, and the /vi switch indicates to exclude files containing the next parameter, the search being case insensitive.
As Joey pointed out, the $ is necessary to indicate end of the line.

Resources