9

As the title suggests, what's the fastest (accurate) way of finding which lines of which files match a particular string.

Specifically, I'm searching .cpp and .h files.

1

12 Answers 12

12

Notepad++ has 'find in files' option, which is located in the 'Search' menu.. This will allow you to search multiple files in a folder (you can specify the folder) and will show you the line numbers of each file as well. You can toggle whether subfolders and hidden folders are included in the search, and it will also allow you to search with \t \n etc. and to search with regular expressions if you need.

Notepad++ is a Windows programmers editor (not an IDE) and is Open Source too.

(i realise that you have most likely heard of notepad++, but I'm just mentioning it here as an option incase you were unaware of this particular feature, and for others who stumble across this thread later on.

Cheers :)

3
  • Yeah, I must admit, I've used Notepad++ for this before.
    – Jon Cage
    Jun 13 '12 at 13:43
  • 1
    @JonCage there's somebody in this thread sevenforums.com/software/… that also speaks like you saying "I admit" when it comes to useful things. That's an extremely odd way of speaking. You might want to meet that person. Maybe your twin.
    – barlop
    Jun 13 '12 at 15:45
  • Notepad++ is great. Other programmer's editors like SciTE have similar functionality as well. Jun 18 '12 at 20:55
5

Agent Ransack works like a charm. Download it here.

1
5

Findstr always works for me. It can use text * wildcards or regex for the search string. Findstr /? for more

5

On Unix systems people would most likely use grep to do this, so why not use Windows Grep?

http://www.wingrep.com/

3
  • Windows Grep can sometime become unstable, but it is the fastest searching program I have ever used.
    – yakatz
    Jun 13 '12 at 20:32
  • @yakatz Thanks for mentioning possible instability with Windows Grep, I will look for a different grep/replace tool! May 22 '14 at 7:06
  • wingrep is discontinued, however grepwin is still available and works very well.
    – techturtle
    Mar 9 '17 at 17:25
3

Here's a Powershell solution:

gci -r $SomePath -filter "*.cpp","*.h" | Select-String $FindThisString

The -r in gci is for recurse, and of course is optional, as are the two file extension filters. A useful parameter of Select-String is -list which will only report the first instance of a match in a file, instead of giving you each occurrence of it per file.

Perhaps you want to instantly open each file where a match is found? Well, the following small change to the above is quite useful, depending on what you have your default file association as:

gci -r $SomePath -filter "*.cpp","*.h" | Select-String $FindThisString -list | ii

2
  • I was about to post the same thing :) Jun 13 '12 at 21:14
  • Are you sure -filter "*.cpp","*.h" does accept multiple extensions? This should rather read -include ("*.cpp","*.hpp")
    – mloskot
    Mar 28 '18 at 9:03
2

while doing a localization of some SW I use DocFetcher to search the same strings in previously translated files. It is free and open-source, but needs to create index first. Just try it. :-)

1

Use "strings" provided by M$. They bought some very good utilities from Sysinternals a few years back. Doesn't require installation, just drop it on the box and run it for syntax. Easy to use and GREAT!

1

Another grep implementation for Windows by the famous tortoise svn developers:

grepWin

0

If you are happy on the command line/with Linux. Might I suggest looking at installing a version of grep for windows?

cygwin is a fairly handy way to got about that and will give you a full Linux environment, with bash shell and a rxvt terminal. Otherwise if you just want the tools there is gnuwin32 but then you will be stuck with the Windows cmd.exe prompt.

Microsoft actually have their own version of the Unix tools as a download, but they seemed fairly slow compared to the gnu ports when I tried them. They also going to scrap them with Windows 8.

0

This is how I search for text strings in the Windows Registry or files on my Windows XP machine. Let's assume I want to find the string "Enterprise" in a plain text file on the c: drive, then write the output to a file called search_result.txt in c:\windows\temp

Open a command prompt then type:

findstr "Enterprise" c:*.txt > c:\windows\temp\search_result.txt

The string you search for is case sensitive. "Enterprise" and "enterprise" will not find the same string.

0

Total Commander (with app named Everything) has option to search in files (Alt+F7). Everything speeds up the process of finding files and then TC looks into each file. Wildcards for files and RegExp for text are allowed.

TC: https://www.ghisler.com/download.htm

Everything: https://www.voidtools.com/downloads/

0

You can use /I to make it case insensitive:

findstr /I "Enterprise" c:*.txt

Also, you can write multiple such keywords in a file [say the file name is Patter.txt]

findstr /I /G:pattern.txt  c:*.txt

Content of pattern.txt:

Enterprise\r\nExitprise\r\nabcd

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.