Forum Discussion

Fred_Elmendorf's avatar
Fred_Elmendorf
Brass Contributor
Apr 04, 2023
Solved

Using PowerShell to search a folder of text files for a specific pattern

I'm still just beginning to learn PowerShell, but appreciate all the help I've received so far. This is a fairly urgent need for my work today. Thanks in advance for any help!

 

I need to search the contents all files (plain text files) with the .inf extension, in the folder \\mysystem\Event to identify the string T200, then export a list of the filenames where the string occurs to an .xlsx file. Here is an example of an .inf file.

 

 

file name: example.inf

file contents:

[Summary Information]
Company=MyCompany
RecID=4
FaultName=F3600
When=01/12/2023-13:50:20.974062
IsDayLightSaving=0
TimeCode=-6t
TimeQuality=0
Events=T4,T5,T10,T122,T127,T128,T183,X,E40,T201,T200,
Type=T
IsLt=N
IsTestRun=N
IsLogF=Y
Prefault=250.001
Postfault=1499.999
Length=1750.000
IsFaultLocExist=N
FileExt=inf,cfg,dat
Version=V3.2.1

  • Fred_Elmendorf 

     

    Hi, Fred.

     

    Something like the example below will suffice.

     

    You do not have to worry about commas being in the file names as the output from Export-Csv encloses the values in double quotes, meaning the commas will not interfere with the CSV/Excel formatting.

     

     

    Get-ChildItem -Path "D:\Data\Temp\*" -Filter "*.inf" |
        ForEach-Object {
            $File = $_;
            if ((Get-Content -Path ($_.FullName) -Raw) -match "T200($|[^0-9])") {
                [PSCustomObject] @{
                    Filename = $File.FullName;
                }
            }
        } |
        Export-Csv -NoTypeInformation -Path "D:\Data\Temp\blah.csv"

     

     

    Since .inf files are relatively small (in modern day terms), I've used the -Raw parameter within the call to Get-Content to minimise unnecessary repetitive calls to the "-match" function but if your .inf files are many megabytes in size, you could remove this.

     

    I've also put a small pattern match after the T200 to exclude potential unintentional sub-matches, such as you might otherwise get with T200 being a substring of T2000. Again, change this as you see fit.

     

    Cheers,

    Lain

3 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Fred_Elmendorf 

     

    Hi, Fred.

     

    Something like the example below will suffice.

     

    You do not have to worry about commas being in the file names as the output from Export-Csv encloses the values in double quotes, meaning the commas will not interfere with the CSV/Excel formatting.

     

     

    Get-ChildItem -Path "D:\Data\Temp\*" -Filter "*.inf" |
        ForEach-Object {
            $File = $_;
            if ((Get-Content -Path ($_.FullName) -Raw) -match "T200($|[^0-9])") {
                [PSCustomObject] @{
                    Filename = $File.FullName;
                }
            }
        } |
        Export-Csv -NoTypeInformation -Path "D:\Data\Temp\blah.csv"

     

     

    Since .inf files are relatively small (in modern day terms), I've used the -Raw parameter within the call to Get-Content to minimise unnecessary repetitive calls to the "-match" function but if your .inf files are many megabytes in size, you could remove this.

     

    I've also put a small pattern match after the T200 to exclude potential unintentional sub-matches, such as you might otherwise get with T200 being a substring of T2000. Again, change this as you see fit.

     

    Cheers,

    Lain

    • Fred_Elmendorf's avatar
      Fred_Elmendorf
      Brass Contributor
      Hi, Lain.

      Yes! This gets me what I need.

      Thank you so much.

      Cheers,
      Fred
  • My file names have spaces and commas in them as in this example:

    \\MySystem\eventfolder\My file names,123,xyz.inf
    \\MySystem\eventfolder\My file names,874,rqs.inf

Resources