2

Is it possible to make the Windows find command output ONLY when there are results found? Having such simple search command:

find "str1" *.* 

regardless of the "str1" string is found or not the find always outputs the file name scanned, like this:

---------- FILE1.TXT

---------- FILE2.TXT
This is a line in file2.txt that contains str1.

---------- FILE3.TXT

I'd like to see ONLY the files that contain the string "str1".

1
  • What's the latest on your question regarding the answer below which I provided? Have you been able to test and consider this solution? Feb 14 '21 at 22:09
0

Use findstr rather than find to get the file name and line content of matches only. Or use it to get the file name only of any file(s) that contain any number of matches.

This solution doesn't require complex batch logic or putting the find command in a loop to parse output and such to get the expected result(s).

Command1

findstr "str1" *.*

Output

File.txt:"something","str1","something else"
File.txt:"str1","same file","different line"
File2.txt:"some line","str1","different file"
File9.txt:"another line","different file","str1

Output Explained

File.ext:display whole line containing matching string

Note: This shows the file name each time for each line in any file that a matching string is found—and it also displays the line content which the matching string is found.


Command 2

findstr /M "str1" *.*

Output

File.txt
File2.txt
File9.txt

Note: This shows the file name only and only once for each matching file containing any matching string within its content.

Supporting Resources

  • Findstr

    /I   Case-insensitive search.    
    /S   Search subfolders.
    
1
  • @Daros911 If you wanted to get a better result you have many more options with PowerShell but since you are asking about finding a string with batch and the find command in particular, I took a stab at giving you a solution using findstr which is designed and built in with Windows more tuned for the purpose of finding strings in particular than even the find command. Jan 28 '21 at 0:55
-1

You can just use find . -name 'str1' to search recursievly in the current directory.

1
  • 2
    The question is about the Windows find command, which is the Windows equivalent of the grep command.  Your answer is for Unix/Linux.
    – Scott
    Jan 28 '21 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.