Matching file names using regular expressions

Welcome Forums General PowerShell Q&A Matching file names using regular expressions

This topic contains 2 replies, has 3 voices, and was last updated by

6 months, 2 weeks ago.

  • Author
  • #113443

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member


    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?


  • #113455

    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major Contributor

    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'
  • #113461

    Topics: 21
    Replies: 559
    Points: 993
    Helping Hand
    Rank: Major Contributor

    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
    PS C:\> get-childitem [0-9][0-9][0-9].txt | select-string hi

The topic ‘Matching file names using regular expressions’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort