Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Recursively Searching for "text" in files in windows 11

Recursively Searching for "text" in files in windows 11

Scheduled Pinned Locked Moved The Lounge
linuxalgorithmsquestionannouncement
30 Posts 18 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jmaida

    This rather plain vanilla operation in terms of a user view point is surprising complicated in Windows. This is used to be easy to do using grep or some version of it. Hey, Microsoft how about this command search for "text" in all text files "on my disk" and I mean that simple. No ??..xxgreppx */*/.*(*dmmd Grrrr.

    "A little time, a little trouble, your better day" Badfinger

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #8

    Maybe I'm missing some nuance in your post, but have you tried finest?

    Advanced TypeScript Programming Projects

    1 Reply Last reply
    0
    • J jmaida

      This rather plain vanilla operation in terms of a user view point is surprising complicated in Windows. This is used to be easy to do using grep or some version of it. Hey, Microsoft how about this command search for "text" in all text files "on my disk" and I mean that simple. No ??..xxgreppx */*/.*(*dmmd Grrrr.

      "A little time, a little trouble, your better day" Badfinger

      T Offline
      T Offline
      trønderen
      wrote on last edited by
      #9

      I use Notepad++ for searching in a specific file - so I use Notepad++ for searching in all files. It lets me select between case sensitive and insensitive search. It lets me search for whole words only. It lets me search for either plain strings, strings with control characters escaped by backslashes, or a regular expession. It lets me filter files by name and/or extension, with a list of alternatives (such as "*.txt;*.log;*.cs") It lets me select files in a single directory or also in subdirectories. It lets me replace the found string with another text in all matching files. It lets me navigate the directory tree graphically for selecting the directory (tree) to search. It handles files in various encodings, including UTF-8 and different line ending conventions. It lets me fetch two (or more) files with hits, and compare them (with plugin, but a standard one that should always be installed). The hit list is very well organized: It shows a single line for each hit; you can open that file on that line by clicking it. You can temporarily hide all hits in one (or all) files. You can delete files of no interest from the hit list, while continuing to inspect the remaining ones. It is quite fast. But most of all: Using the same tool, with the same dialog fields, for searching a directory tree as the one you use for searching in the one text file you are editing means that there is no new tool to learn, no new command syntax or specification format. It is familiar and friendly. This of course is if np++ already is your standard text file editor. If it is not, my question is "Why not?" :-)

      E J 2 Replies Last reply
      0
      • T trønderen

        I use Notepad++ for searching in a specific file - so I use Notepad++ for searching in all files. It lets me select between case sensitive and insensitive search. It lets me search for whole words only. It lets me search for either plain strings, strings with control characters escaped by backslashes, or a regular expession. It lets me filter files by name and/or extension, with a list of alternatives (such as "*.txt;*.log;*.cs") It lets me select files in a single directory or also in subdirectories. It lets me replace the found string with another text in all matching files. It lets me navigate the directory tree graphically for selecting the directory (tree) to search. It handles files in various encodings, including UTF-8 and different line ending conventions. It lets me fetch two (or more) files with hits, and compare them (with plugin, but a standard one that should always be installed). The hit list is very well organized: It shows a single line for each hit; you can open that file on that line by clicking it. You can temporarily hide all hits in one (or all) files. You can delete files of no interest from the hit list, while continuing to inspect the remaining ones. It is quite fast. But most of all: Using the same tool, with the same dialog fields, for searching a directory tree as the one you use for searching in the one text file you are editing means that there is no new tool to learn, no new command syntax or specification format. It is familiar and friendly. This of course is if np++ already is your standard text file editor. If it is not, my question is "Why not?" :-)

        E Offline
        E Offline
        englebart
        wrote on last edited by
        #10

        Notepad++ also handles multi byte, BOM, BOMless, etc whereas cmd’s FIND /s and FINDSTR /s only work with single byte characters.

        1 Reply Last reply
        0
        • J jmaida

          This rather plain vanilla operation in terms of a user view point is surprising complicated in Windows. This is used to be easy to do using grep or some version of it. Hey, Microsoft how about this command search for "text" in all text files "on my disk" and I mean that simple. No ??..xxgreppx */*/.*(*dmmd Grrrr.

          "A little time, a little trouble, your better day" Badfinger

          R Offline
          R Offline
          raddevus
          wrote on last edited by
          #11

          If you have Linqpad -- LINQPad - The .NET Programmer's Playground[^] -- (and what self-respecting C# dev doesn't :rolleyes: ) then I got your back on this: Here's a great little findInFiles script I wrote a few years ago when i was frustrated because I couldn't search inside of source code (*.cs) to find specific text items I needed. Keep in mind I wrote this very quickly bec I was needing to search in files for specific text. It ignores case and finds all matches ( you can add a parameter to handle this). it will prompt you for a few items: 1. Directory you want to search (searches all subdirs) 2. text you want to search for. 3. file pattern you want to search against *.*, *.cs, *.txt, etc. that's it. It'll go through them all and give you some results. Yes, it's just bruteforce but it works and it's relatively fast and you'll see updated results as it finds the text.

          void Main()
          {
          Console.WriteLine ("Enter the path you want to search.");
          string searchPath = Console.ReadLine();
          Console.WriteLine(string.Format("Searching : {0}", searchPath));
          DirectoryInfo DirInfo = new DirectoryInfo(searchPath);
          Console.Write("Search Term: ");
          string searchTerm = Console.ReadLine().ToUpper();
          Console.WriteLine(searchTerm);
          Console.WriteLine("Enter the file pattern you want search against.");
          string filePattern = Console.ReadLine();
          try
          {
          var files = DirInfo.EnumerateFiles(filePattern,SearchOption.AllDirectories);
          foreach (var f in files)
          {
          // Console.WriteLine($"Searching {Path.GetFileName(f.Name)}"); // uncomment to see all file names searched
          string [] allLines = File.ReadAllLines(f.FullName);
          int lineCount = 1;
          bool foundInFile = false;
          foreach (string line in allLines)
          {
          if (line.ToUpper().Contains(searchTerm))
          {
          if (!foundInFile)
          {
          // insures it only prints filename once
          Console.WriteLine("searching {0}", f.FullName.ToUpper());
          foundInFile=true;
          }
          Console.WriteLine(string.Format("FOUND : {0} {1}",lineCount, line));
          }
          lineCount++;
          }
          if (foundInFile)
          {
          Console.WriteLine("#############################");
          Console.WriteLine();
          }
          }
          }
          finally
          {

          }
          

          }

          B Richard DeemingR 2 Replies Last reply
          0
          • D David ONeil

            [Try Google](https://www.google.com/search?q=grep+for+windows&sourceid=chrome&ie=UTF-8&tbs=li:1) - maybe they are wrong, but it is a start.

            Our Forgotten Astronomy | Object Oriented Programming with C++ | Wordle solver

            J Offline
            J Offline
            jmaida
            wrote on last edited by
            #12

            Google used have a search engine one could use search you own system (can't recall it's exact name google_something). They don't have it anymore. It was great.

            "A little time, a little trouble, your better day" Badfinger

            D 1 Reply Last reply
            0
            • T trønderen

              I use Notepad++ for searching in a specific file - so I use Notepad++ for searching in all files. It lets me select between case sensitive and insensitive search. It lets me search for whole words only. It lets me search for either plain strings, strings with control characters escaped by backslashes, or a regular expession. It lets me filter files by name and/or extension, with a list of alternatives (such as "*.txt;*.log;*.cs") It lets me select files in a single directory or also in subdirectories. It lets me replace the found string with another text in all matching files. It lets me navigate the directory tree graphically for selecting the directory (tree) to search. It handles files in various encodings, including UTF-8 and different line ending conventions. It lets me fetch two (or more) files with hits, and compare them (with plugin, but a standard one that should always be installed). The hit list is very well organized: It shows a single line for each hit; you can open that file on that line by clicking it. You can temporarily hide all hits in one (or all) files. You can delete files of no interest from the hit list, while continuing to inspect the remaining ones. It is quite fast. But most of all: Using the same tool, with the same dialog fields, for searching a directory tree as the one you use for searching in the one text file you are editing means that there is no new tool to learn, no new command syntax or specification format. It is familiar and friendly. This of course is if np++ already is your standard text file editor. If it is not, my question is "Why not?" :-)

              J Offline
              J Offline
              jmaida
              wrote on last edited by
              #13

              Wow, Did not know Notepad++ did this. Thanx, Trønderen and Englebart. I'll try it

              "A little time, a little trouble, your better day" Badfinger

              1 Reply Last reply
              0
              • J jmaida

                Google used have a search engine one could use search you own system (can't recall it's exact name google_something). They don't have it anymore. It was great.

                "A little time, a little trouble, your better day" Badfinger

                D Offline
                D Offline
                David ONeil
                wrote on last edited by
                #14

                I believe you are talking about this: [Google Desktop - Wikipedia](https://en.wikipedia.org/wiki/Google\_Desktop). I think it was known for sending lots of data back to Google, if my memory serves, but everyone liked it. It also became redundant when Windows added about the same thing with its file search capabilities where it databased everything (which is what Google also did I believe). I'll also add my two cents for Agent Ransack, which I use occasionally.

                Our Forgotten Astronomy | Object Oriented Programming with C++ | Wordle solver

                J 1 Reply Last reply
                0
                • L Lost User

                  What about the "find" command, or Select-String - PowerShell - SS64.com[^] in Powershell?

                  J Offline
                  J Offline
                  jmaida
                  wrote on last edited by
                  #15

                  I have used find command, but does not recursive search, but I learned that FINDSTR command does. I'll try it. wish Windows had a more general search engine like a google equivalent built-in.

                  "A little time, a little trouble, your better day" Badfinger

                  J 1 Reply Last reply
                  0
                  • B BillWoodruff

                    Agent Ransack I have not tried this, but, check out: [^]

                    «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

                    J Offline
                    J Offline
                    jmaida
                    wrote on last edited by
                    #16

                    This has been mentioned several times. I will give it try. Thanx

                    "A little time, a little trouble, your better day" Badfinger

                    1 Reply Last reply
                    0
                    • D David ONeil

                      I believe you are talking about this: [Google Desktop - Wikipedia](https://en.wikipedia.org/wiki/Google\_Desktop). I think it was known for sending lots of data back to Google, if my memory serves, but everyone liked it. It also became redundant when Windows added about the same thing with its file search capabilities where it databased everything (which is what Google also did I believe). I'll also add my two cents for Agent Ransack, which I use occasionally.

                      Our Forgotten Astronomy | Object Oriented Programming with C++ | Wordle solver

                      J Offline
                      J Offline
                      jmaida
                      wrote on last edited by
                      #17

                      Yeah, it was Desktop Google. Trying Ransack. Works reasonably well.

                      "A little time, a little trouble, your better day" Badfinger

                      1 Reply Last reply
                      0
                      • R raddevus

                        If you have Linqpad -- LINQPad - The .NET Programmer's Playground[^] -- (and what self-respecting C# dev doesn't :rolleyes: ) then I got your back on this: Here's a great little findInFiles script I wrote a few years ago when i was frustrated because I couldn't search inside of source code (*.cs) to find specific text items I needed. Keep in mind I wrote this very quickly bec I was needing to search in files for specific text. It ignores case and finds all matches ( you can add a parameter to handle this). it will prompt you for a few items: 1. Directory you want to search (searches all subdirs) 2. text you want to search for. 3. file pattern you want to search against *.*, *.cs, *.txt, etc. that's it. It'll go through them all and give you some results. Yes, it's just bruteforce but it works and it's relatively fast and you'll see updated results as it finds the text.

                        void Main()
                        {
                        Console.WriteLine ("Enter the path you want to search.");
                        string searchPath = Console.ReadLine();
                        Console.WriteLine(string.Format("Searching : {0}", searchPath));
                        DirectoryInfo DirInfo = new DirectoryInfo(searchPath);
                        Console.Write("Search Term: ");
                        string searchTerm = Console.ReadLine().ToUpper();
                        Console.WriteLine(searchTerm);
                        Console.WriteLine("Enter the file pattern you want search against.");
                        string filePattern = Console.ReadLine();
                        try
                        {
                        var files = DirInfo.EnumerateFiles(filePattern,SearchOption.AllDirectories);
                        foreach (var f in files)
                        {
                        // Console.WriteLine($"Searching {Path.GetFileName(f.Name)}"); // uncomment to see all file names searched
                        string [] allLines = File.ReadAllLines(f.FullName);
                        int lineCount = 1;
                        bool foundInFile = false;
                        foreach (string line in allLines)
                        {
                        if (line.ToUpper().Contains(searchTerm))
                        {
                        if (!foundInFile)
                        {
                        // insures it only prints filename once
                        Console.WriteLine("searching {0}", f.FullName.ToUpper());
                        foundInFile=true;
                        }
                        Console.WriteLine(string.Format("FOUND : {0} {1}",lineCount, line));
                        }
                        lineCount++;
                        }
                        if (foundInFile)
                        {
                        Console.WriteLine("#############################");
                        Console.WriteLine();
                        }
                        }
                        }
                        finally
                        {

                        }
                        

                        }

                        B Offline
                        B Offline
                        BillWoodruff
                        wrote on last edited by
                        #18

                        Wow ! thanks ... May I suggest you publish this as a Tip/Trick, or flesh iit out a little bit and publish as article. I can "see" adding an Enum that would filter on either lower or upper case matches, or both. cheers, Bill

                        «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

                        R 1 Reply Last reply
                        0
                        • J jmaida

                          This rather plain vanilla operation in terms of a user view point is surprising complicated in Windows. This is used to be easy to do using grep or some version of it. Hey, Microsoft how about this command search for "text" in all text files "on my disk" and I mean that simple. No ??..xxgreppx */*/.*(*dmmd Grrrr.

                          "A little time, a little trouble, your better day" Badfinger

                          J Offline
                          J Offline
                          Jorgen Andersson
                          wrote on last edited by
                          #19

                          I'm using Total Commander for that

                          Wrong is evil and must be defeated. - Jeff Ello

                          J 1 Reply Last reply
                          0
                          • J Jorgen Andersson

                            I'm using Total Commander for that

                            Wrong is evil and must be defeated. - Jeff Ello

                            J Offline
                            J Offline
                            Jorgen Andersson
                            wrote on last edited by
                            #20

                            Supports search by ANSI, ASCII, UTF8, UTF16, Office XML, EPUB, HEX and REGEX.

                            Wrong is evil and must be defeated. - Jeff Ello

                            1 Reply Last reply
                            0
                            • J jmaida

                              This rather plain vanilla operation in terms of a user view point is surprising complicated in Windows. This is used to be easy to do using grep or some version of it. Hey, Microsoft how about this command search for "text" in all text files "on my disk" and I mean that simple. No ??..xxgreppx */*/.*(*dmmd Grrrr.

                              "A little time, a little trouble, your better day" Badfinger

                              G Offline
                              G Offline
                              GuyThiebaut
                              wrote on last edited by
                              #21

                              I've been using Agent Ransack for over 15 years and it's still a great piece of software for searching both by file name and within files.

                              “That which can be asserted without evidence, can be dismissed without evidence.”

                              ― Christopher Hitchens

                              1 Reply Last reply
                              0
                              • R raddevus

                                If you have Linqpad -- LINQPad - The .NET Programmer's Playground[^] -- (and what self-respecting C# dev doesn't :rolleyes: ) then I got your back on this: Here's a great little findInFiles script I wrote a few years ago when i was frustrated because I couldn't search inside of source code (*.cs) to find specific text items I needed. Keep in mind I wrote this very quickly bec I was needing to search in files for specific text. It ignores case and finds all matches ( you can add a parameter to handle this). it will prompt you for a few items: 1. Directory you want to search (searches all subdirs) 2. text you want to search for. 3. file pattern you want to search against *.*, *.cs, *.txt, etc. that's it. It'll go through them all and give you some results. Yes, it's just bruteforce but it works and it's relatively fast and you'll see updated results as it finds the text.

                                void Main()
                                {
                                Console.WriteLine ("Enter the path you want to search.");
                                string searchPath = Console.ReadLine();
                                Console.WriteLine(string.Format("Searching : {0}", searchPath));
                                DirectoryInfo DirInfo = new DirectoryInfo(searchPath);
                                Console.Write("Search Term: ");
                                string searchTerm = Console.ReadLine().ToUpper();
                                Console.WriteLine(searchTerm);
                                Console.WriteLine("Enter the file pattern you want search against.");
                                string filePattern = Console.ReadLine();
                                try
                                {
                                var files = DirInfo.EnumerateFiles(filePattern,SearchOption.AllDirectories);
                                foreach (var f in files)
                                {
                                // Console.WriteLine($"Searching {Path.GetFileName(f.Name)}"); // uncomment to see all file names searched
                                string [] allLines = File.ReadAllLines(f.FullName);
                                int lineCount = 1;
                                bool foundInFile = false;
                                foreach (string line in allLines)
                                {
                                if (line.ToUpper().Contains(searchTerm))
                                {
                                if (!foundInFile)
                                {
                                // insures it only prints filename once
                                Console.WriteLine("searching {0}", f.FullName.ToUpper());
                                foundInFile=true;
                                }
                                Console.WriteLine(string.Format("FOUND : {0} {1}",lineCount, line));
                                }
                                lineCount++;
                                }
                                if (foundInFile)
                                {
                                Console.WriteLine("#############################");
                                Console.WriteLine();
                                }
                                }
                                }
                                finally
                                {

                                }
                                

                                }

                                Richard DeemingR Offline
                                Richard DeemingR Offline
                                Richard Deeming
                                wrote on last edited by
                                #22

                                Be careful - if your account doesn't have permission to read any of the folders in the search path, the script will fail at the first one. In .NET Framework, there's no way to make a SearchOption.AllDirectories search skip folders you don't have access to. If you're using .NET Core 2.1 or later (including .NET 5/6/7/...), you can use the EnumerationOptions[^] class with the IgnoreInacessible property set to true to resolve this:

                                EnumerationOptions options = new()
                                {
                                RecurseSubdirectories = true,
                                IgnoreInaccessible = true,
                                };

                                var files = DirInfo.EnumerateFiles(filePattern, options);


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                R 1 Reply Last reply
                                0
                                • B BillWoodruff

                                  Wow ! thanks ... May I suggest you publish this as a Tip/Trick, or flesh iit out a little bit and publish as article. I can "see" adding an Enum that would filter on either lower or upper case matches, or both. cheers, Bill

                                  «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

                                  R Offline
                                  R Offline
                                  raddevus
                                  wrote on last edited by
                                  #23

                                  That's a great idea and I will try to get around to it. Thanks :thumbsup:

                                  1 Reply Last reply
                                  0
                                  • Richard DeemingR Richard Deeming

                                    Be careful - if your account doesn't have permission to read any of the folders in the search path, the script will fail at the first one. In .NET Framework, there's no way to make a SearchOption.AllDirectories search skip folders you don't have access to. If you're using .NET Core 2.1 or later (including .NET 5/6/7/...), you can use the EnumerationOptions[^] class with the IgnoreInacessible property set to true to resolve this:

                                    EnumerationOptions options = new()
                                    {
                                    RecurseSubdirectories = true,
                                    IgnoreInaccessible = true,
                                    };

                                    var files = DirInfo.EnumerateFiles(filePattern, options);


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                    R Offline
                                    R Offline
                                    raddevus
                                    wrote on last edited by
                                    #24

                                    That's a great tip. thanks for adding. :thumbsup:

                                    1 Reply Last reply
                                    0
                                    • J jmaida

                                      This rather plain vanilla operation in terms of a user view point is surprising complicated in Windows. This is used to be easy to do using grep or some version of it. Hey, Microsoft how about this command search for "text" in all text files "on my disk" and I mean that simple. No ??..xxgreppx */*/.*(*dmmd Grrrr.

                                      "A little time, a little trouble, your better day" Badfinger

                                      R Offline
                                      R Offline
                                      rjmoses
                                      wrote on last edited by
                                      #25

                                      Several 3rd party tools I keep available include "Windows Grep", grepWin and SearchMonkey. SearchMonkey and grepWin are my favorites. Both support regular expression searches.

                                      J 1 Reply Last reply
                                      0
                                      • R rjmoses

                                        Several 3rd party tools I keep available include "Windows Grep", grepWin and SearchMonkey. SearchMonkey and grepWin are my favorites. Both support regular expression searches.

                                        J Offline
                                        J Offline
                                        jmaida
                                        wrote on last edited by
                                        #26

                                        GREP AND GREPWIN I HAVE USED BUT NOT WITHOUT ISSUES. SEARCH MONKEY i WILL TRY. I TRIED RANSACK. PRETTY GOOD. WINDOWS DOES FINDSTR COMMAND BUT ONLY AT COMMAND PROMPT LEVEL. I KNOW THERE ARE MANY WAYS, MY ANGST IS THAT WINDOWS HAS NOT DONE THEIR OWN VERY WELL. I DON'T WANT TO HAVE TO THINK TOO MUCH EVERY TIME I USE IT. IT SHOULD BE INTUITIVE.

                                        "A little time, a little trouble, your better day" Badfinger

                                        1 Reply Last reply
                                        0
                                        • J jmaida

                                          This rather plain vanilla operation in terms of a user view point is surprising complicated in Windows. This is used to be easy to do using grep or some version of it. Hey, Microsoft how about this command search for "text" in all text files "on my disk" and I mean that simple. No ??..xxgreppx */*/.*(*dmmd Grrrr.

                                          "A little time, a little trouble, your better day" Badfinger

                                          J Offline
                                          J Offline
                                          Jeremy Falcon
                                          wrote on last edited by
                                          #27

                                          If you have Git for Windows installed already then you have the POSIX ports of grep and find for Windows already installed, it's just most likely they are not in your PATH to avoid name clashes. You can use it outside of Git Bash in CMD or PowerShell as it's just a Windows port.

                                          C:\Program Files\Git\usr\bin\find.exe
                                          C:\Program Files\Git\usr\bin\grep.exe

                                          If you don't have Git for Windows installed, install it. No developer should be without Git these days. Also, WSL2 is great, if you want to use a full-on environment. Better than the old Cygwin days. I say this as a dude that used to be all about PowerShell. If you're going to take the time to learn something may as well learn something cross platform that you can use on a Mac as well.

                                          Jeremy Falcon

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups