Matching file names using regular expressions

Hi,

I have a directory in which I have text files repesenting pages of books, named according to a convention.
Files from 0001.txt through 1239.txt are book A;
files 000.txt through 847.txt are book B;
files t000.txt through t498.txt are book C.

I want to use findstr in this directory to find a search text “two rivers” in book B only (so not in any of the files whose names contain four digits). In bash I could do this:

$ grep "two rivers" [0-9][0-9][0-9].txt

This method of pattern matching on file names doesn’t seem to work in PowerShell.

C:\Users\P\source> findstr "two rivers" [0-9][0-9][0-9].txt
FINDSTR: Cannot open [0-9][0-9][0-9].txt

Is there a way to make this work?

 

Give something like this a shot. Not as simple, but if you wanted you could wrap a function around it if you used it often.

Get-Childitem -Path 'C:\Scripts\Test' | 
Where{$_.Name -match '^F[0-9]{5}.txt$'} |
Select-String -Pattern 'BLUE'

The pattern you can do [0-9]{3} to specify it has to be 0-9 and exactly 3 characters versus [0-9][0-9][0-9]. The pattern above is a for files F00001.txt, F00002.txt, etc, which could be easily modified. I picked a file and put “BLUE” in the file, so my output looks like this:

PS C:\Users\Rob> Get-Childitem -Path 'C:\Scripts\Test' | Where{$_.Name -match '^F[0-9]{5}.txt$'}



    Directory: C:\Scripts\Test


Mode                LastWriteTime         Length Name                                                                                                                                                                  
----                -------------         ------ ----                                                                                                                                                                  
-a----        7/27/2018   1:43 PM              0 F00001.txt                                                                                                                                                            
-a----        7/27/2018   1:44 PM              0 F00002.txt                                                                                                                                                            
-a----        7/27/2018   1:44 PM              0 F00003.txt                                                                                                                                                            
-a----        7/27/2018   1:44 PM              0 F00004.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00005.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00006.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00007.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00008.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00009.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00010.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00011.txt                                                                                                                                                            
-a----        10/7/2018   7:45 PM              4 F00012.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00013.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00014.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00015.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00016.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00017.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00018.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00019.txt                                                                                                                                                            
-a----        7/27/2018   1:45 PM              0 F00020.txt                                                                                                                                                            



PS C:\Users\Rob> Get-Childitem -Path 'C:\Scripts\Test' | 
Where{$_.Name -match '^F[0-9]{5}.txt$'} |
Select-String -Pattern 'BLUE'

C:\Scripts\Test\F00012.txt:1:BLUE

It works for me, but only with powershell commands. Those patterns work as .net wildcards.

PS C:\> set-content hi 123.txt

PS C:\> findstr hi [0-9][0-9][0-9].txt
FINDSTR: Cannot open [0-9][0-9][0-9].txt

PS C:\> select-string hi [0-9][0-9][0-9].txt

123.txt:1:hi


PS C:\> get-childitem [0-9][0-9][0-9].txt | select-string hi

123.txt:1:hi