Computing: Computer Basics

Home and Office: Searching a directory for given files or file content.

When writing my tutorial about using Javascript to hide/show given text paragraphs, I had to look up examples of the usage of the corresponding script in my website files. With a rather huge number of HTML files, I did not remember in which of them I used the script. So, I decided to search all HTML files on my site in order to find those containing the text "<script". To do this, I had to find some software that allows to search a complete directory structure (here my Apache document root plus its subdirectories) for given files (here: HTML files), and that is able to find given content (here the text "<script") in all these files. Having such a tool installed on your computer may be very useful and helpful, in particular for programmers, but also for mainstream users, for example to find a given word, expression, or part of a sentence in a series of articles.

You can search for all files with given search criteria (for example, all .html files) in Windows File Explorer. This search includes by default all subdirectories. You can use the wildcards "*" and "?"; regular expressions are not supported.

Finding all files with given search criteria in a directory and all its subdirectories is also easy to do in Command Prompt using the dir command with the /s option. And the file listing can be written to a text file using redirection. For example, to list all .html files on my webserver and store the list the in the file filelist.txt, I could use the following commands:
    c:
    cd \Programs\Apache24\htdocs
    dir *.html /s >filelist.txt

A simple way to search for a given text (or regular expression) in several files is using the Find All in All Opened Documents option of the Notepad++ Find command. All nice, but the dir command cannot be used to search for file content, and Notepad++ may only be used with files opened within the application.

Microsoft Windows includes lots of command line tools for lots of different tasks. Unfortunately, most users don't ever use them, because they don't know that they exist. Or have you ever heard of Findstr? It is part of Windows 10 (and probably all modern Windows releases) and does exactly what we need to do here: Searching a directory structure for files with given content. For details about this program, please have a look at learn.microsoft.com.

Using Command Prompt is not what mainstream users are used to, and finding a GUI application for this task would be a good thing. One possibility would be to buy PowerGREP for only 159€, as they call it on their website. But, the time, where you have to pay for quality software is over for a long time, and whatever you need, it should always be possible to find a freeware application. I found GrepWin, entirely free, with lots of search options, and really fast. And, you can not only search for a text or regex, but also replace them! I downloaded the Windows 64-bit installer from chip.de. This computer website is in German, but you can use Google Translate, if you don't understand this language; also, the GrepWin application itself is in English.

The installation is straight forward. If you do a standard installation using the Install button, the only user intervention required is the license agreement. Note, that the default installation folder is not "C:\Program Files\grepWin", but C:\Users\<user-name>\AppData\Local\Programs\grepWin.

GrepWin: Installation - License agreement

On chip.de, they say that the disadvantage of this software is a somewhat confusing GUI. There are a lot of options, it's true, but I think that even a beginner should be able to use this great software without any problems. The screenshot below shows how I search the Apache document root with all its subdirectories (note the selected checkbox Include subfolders) for HTML files (specified by "*.html" in the File names match field), for the text (corresponding radio button checked) "<script" (to be entered in the Search for field). And the most important setting: Be sure to select the radio button Content at the bottom of the window! Over 4500 files in the directory structure, 855 of them being HTML documents (where the search is actually done); among these, 139 that include the search string. To note that on my DELL notebook, the search just took a second!

GrepWin: Searching the entire Apache htdocs for HTML files including a Javascript reference

If you find this text helpful, please, support me and this website by signing my guestbook.