Home > Back-end >  Get-Content using Regex on the filename content
Get-Content using Regex on the filename content

Time:06-23

I want to print the ouput of a file matching a regex (Get-Content), with the concern that I'm looking (Get-ChildItem) the File using Regex too.

-File example: ITOPS_Log [2022-06-18].txt

-File content:QQQ-9999999-QQQ

#Find the File using Regex: 
$folder = "C:\Users\Eddy\Desktop"

  $valid_files = Get-ChildItem $folder| Where-Object { $_.Name -match 'ITOPS_Log.\[\d{4}-\d{2}-\d{2}\].txt' }
  Write-Output $valid_files

 #Read the file and print content.
     Foreach ($file in $valid_files) {
         $content = (Get-Content $file.FullName)
          ForEach ($line in $content) {
          Write-Output "$line"
         }
     }

Output:

Mode                 LastWriteTime         Length Name                                                                                                                                                         
----                 -------------         ------ ----                                                                                                                                                         
-a----        22/06/2022     16:11             64 ITOPS_Log [2022-06-18].txt 

Get-Content : An object at the specified path C:\Users\Eddy\Desktop\ITOPS_Log [2022-06-18].txt does not exist, or has been filtered by the -Include or -Exclude parameter.
At C:\Users\Eddy\Desktop\Itops_Log_test_V2.ps1:15 char:25
              $content = (Get-Content $file.FullName)
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
      FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand

I know I'm not using Regex to match with the content if only want the number. But I got stuck already without filters and I need to solve this first and then apply regex to match the digits.

How can I print the ouput of the File with this code? I don't understand what I'm missing.

CodePudding user response:

Continuing from my comment, The -Path parameter on Get-ChildItem and Get-Content tries to resolve wildcard characters and because your file has square brackets, it sees that as a range of characters or numbers.

To avoid that, use -LiteralPath instead so nothing in the path gets interpreted.

Then to test if the file has something resembling a date inside those square brackets, I would use an anchored regex on the file's BaseName property:

#Find the File using Regex: 
$folder = "C:\Users\Eddy\Desktop"

$valid_files = Get-ChildItem -LiteralPath $folder -Filter 'ITOPS_Log*.txt' -File | 
               Where-Object { $_.BaseName -match '\[\d{4}-\d{2}-\d{2}\]$' }
# show the found files on screen
$valid_files

#Read the file and print content.
foreach ($file in $valid_files) {
    $content = (Get-Content -LiteralPath $file.FullName)
    foreach ($line in $content) {
        Write-Host $line
        # or just the number?
        Write-Host ([regex]'(\d )').Match($line).Groups[1].Value
    }
}

Regex details on the file's BaseName (--> File Name without extension):

\[         Match the character “[” literally
\d         Match a single digit 0..9
   {4}     Exactly 4 times
-          Match the character “-” literally
\d         Match a single digit 0..9
   {2}     Exactly 2 times
-          Match the character “-” literally
\d         Match a single digit 0..9
   {2}     Exactly 2 times
\]         Match the character “]” literally
$          Assert position at the end of the string (or before the line break at the end of the string, if any)

CodePudding user response:

#Find the File using Regex: 
$folder = "C:\Users\Eddy\Desktop"

  $valid_files = Get-ChildItem $folder| Where-Object { $_.Name -match 'ITOPS_Log.\[\d{4}-\d{2}-\d{2}\].txt' }
  Write-Output $valid_files

  Write-Output $valid_files

 #Read the file and print content.
     Foreach ($file in $valid_files) {
     
         $content = (Get-Content -LiteralPath $folder\$file)
          ForEach ($line in $content) {
          Write-Output "$line"
         }
     }

  • Related