Searching for Strings and Regular Expressions in Multiple Files on Prehistoric Windows

Before Windows got its WSL swagger installing grep was cumbersome (but at least it was possible, thanks for the memories, Cygwin!)

That being said, you could count on Rando McClient's winb0x to ship with with its own take, the command line tools find:

c:\>find /? Searches for a text string in a file or files. FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" drive:][path]filename[ ... /V Displays all lines NOT containing the specified string. /C Displays only the count of lines containing the string. /N Displays line numbers with the displayed lines. /I Ignores the case of characters when searching for the string. /OFF[LINE] Do not skip files with offline attribute set. "string" Specifies the text string to find. [drive:][path]filename Specifies a file or files to search. If a path is not specified, FIND searches the text typed at the prompt or piped from another command.

... and findstr:

c:\>findstr /? Searches for strings in files. FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]] strings drive:][path]filename[ ... /B Matches pattern if at the beginning of a line. /E Matches pattern if at the end of a line. /L Uses search strings literally. /R Uses search strings as regular expressions. /S Searches for matching files in the current directory and all subdirectories. /I Specifies that the search is not to be case-sensitive. /X Prints lines that match exactly. /V Prints only lines that do not contain a match. /N Prints the line number before each line that matches. /M Prints only the filename if a file contains a match. /O Prints character offset before each matching line. /P Skip files with non-printable characters. /OFF[LINE] Do not skip files with offline attribute set. /A:attr Specifies color attribute with two hex digits. See "color /?" /F:file Reads file list from the specified file(/ stands for console). /C:string Uses specified string as a literal search string. /G:file Gets search strings from the specified file(/ stands for console). /D:dir Search a semicolon delimited list of directories strings Text to be searched for. [drive:][path]filename Specifies a file or files to search. Use spaces to separate multiple search strings unless the argument is prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y. Regular expression quick reference: . Wildcard: any character * Repeat: zero or more occurrences of previous character or class ^ Line position: beginning of line $ Line position: end of line [class] Character class: any one character in set [^class] Inverse class: any one character not in set [x-y] Range: any characters within the specified range \x Escape: literal use of metacharacter x \ Word position: end of word For full information on FINDSTR regular expressions refer to the online Command Reference.

It is also possible to leverage the Windows Explorer shell by navigating to a folder containing the file or files you wish to search and entering your query into the search box integrated into the top-right corner of the window. You can improve the results and reduce query time/resource load by filtering the file type or file name with the file: modifier and conventional Windows filename wildcards like ?, _ and *. For example, to limit the query to plaintext files one would append file: *.txt after the search term(s), separated by a space.

Explorer's Search functionality is virtually identical from v.7 through 11.

I compiled a small testing sample of various formats and some ZIP archives out of the first two acts of Shakespeare's Antony and Cleopatra, with gratitude to MIT.

Explorer didn't adopt Ribbon graphical widgets until v. 2008 but Folder Options has managed searching inside ZIP files since XP.

The ability to search inside of ZIP files appeared with the introduction of Windows XP and its broad, built-in support abstract "Compressed Folders" based on the format.

Cabinet (.CAB) Files support multiple compression algorithms but are usually encoded similarly to .ZIPs

...Searching inside archive files can also be enabled through Folder Options which can be accessed via Explorer's View Tab > View Ribbon > Options Button or...

A side-by-side comparison of Windows 10's apparently stand-alone File Folder Options and File Explorer's Folder Options

...you can modify the exact same settings in what appears to be the exact same interface, other than the title: File Folder Options which is easily dredged up by way of the the taskbar's integrated Search Bar or the reduced-width Search Button or, if you have wisely deleted it altogether: Meta (Windows) Key & Start Typing...

I personally tend to disable Indexing, especially on SSD/Flash backed storage.

For improved performance going forward you may wish to take this opportunity to (same as above) Search for Indexing Options then click the Advanced Button > File Types Tab and find the 'zip' Entry and change the How should this file be indexed? radio button to Index Properties and File Contents; rinse and repeat for any of the other file formats Windows thinks it can handle...

WIndows has an impressive ability to search the contents of many formats./figcaption>

Unfortunately Windows does not support Search and Replace natively. Therefore if you need that ability in conjunction with multi-file search you must adopt a third party solution.

If one is to use a GUI-first operating system I think it's only appropriate to exercise its strengths; running operations against groups of files should be any GUI's cakewalk, especially with some arbitrary file and recursive directory selection/exclusion. I can't tell you how much I wish I could slap a mouse and control/shift keys on the Linux find command some days (fortunately KATE has me covered where interfacing through NFS/FUSE sshfs is feasible). If you often find yourself having to search multiple files at once, as one certainly does in analytical, systems/network administration and software development roles, I have recently had cause to evaluate these two free, GPLv3 solutions for Windows environments:

Side-by-Side Comparison of grepWin and AstroGrep

grepWin (Downloads | Source @ GitHub) will only run on Windows 7 SP1 and above. You have the option of 64 and 32 bit conventional installers or a 1.1MB (uncompressed) portable executable which makes it perfectly suited to bootable USB and DVD environments, PXE or staging on a remote server or site (like this one! :o) for rapid access in the field. Supports PERL-inspired (PCRE) Regular Expressions (RegEx) and recommends a third party a tutorial here, while the software has a powerful on the CLI.

grepWin substantially outperforms AstroGrep

The first thing I noticed running grepWin against my sample set was an at-least 2x possibly three-fold reduction in processing time compared to AstroGrep, having run the same search pattern with it a dozen times prior. An identically matching set across text and RTF files was acquired, only among the plain text (*.TXT) and rich text (*.RTF) files. String replace functionality was fast and I was admonished both to enable backups and disable UTF-8 mode (which I had changed from the default off value; while that shouldn't have caused any issues it is nice to know that an appropriate level of hand-holding is implemented before executing a potentially destructive and ultimately embarrassing act.

AstroGrep: Does what it claims to. And not a penny more.

AstroGrep (Downloads | Source @ SourceForge) has a long history and an established userbase. AstroGrep adds a context menu to Explorer when you right-click on directories while grepWin adds a context menu for individual files. Even if your main use-case substantially differentiates between their featuresets or if the lack of replace functionality is a deal-killer (as it sadly is for me) that could be enough utility to warrant installing both.

Out of the box AstroGrep was able to parse the plain text (*.TXT) and rich text format (*.RTF) files from my sample set. Enabling the "File Handler" plugin added the two OpenDocument Format (*.ODF) files but got nothing out of the HTML, PDFs or the files in archives (as expected). Unfortunately, while it could see the matches I could not: despite the plugin apparently parsing the ODF format enough to find text strings, the reproduction inside of AstroGrep's interface was binary and largely unintelligible.

According to this thread from 2017 that appears to document a discussion between to developers a Replace functionality may have been planned but in the intervening five years apparently that has gone nowhere. A test version of the app (in "portable" form) is provided in the thread; after completing your search the greyed out "fast-forward" button at the bottom of the search form in the left panel of the UI becomes slightly less grey; clicking on it reveals the Replace field. While the replace procedure did work, it only worked for my plain text (*.TXT) files; the rich texts and ODF remained unchanged.

I was hoping I would be able to recommend both applications but the lack of a replace function in AstroGrep - at least for now - cements my personal endorsement with winGrep.


There are no comments for this item.