powershellrename-item-cmdlet

Powershell rename text files based on strings in text --> More concise way for script?


I am trying to rename bankstatements in MT940-format using the account number and the statement date.

The statements contain the following (example):

    :20:
    :25:MHCBNL2AXXX/**0364525123**
    :28C:27/
    :60F:C200207EUR100000,00
    :61:2012311231D0000,1NMSCTOPF1234567890SDD TOPF1234567890
    :86:FR1234567890ARVAL FRANCE
    :62F:C**200207**EUR100000,00

I have written the following powershell script by combining some examples but it seems quite long for the purpose. Question: Is there a concise way to write this script?

 $files = Get-ChildItem "C:\Dropbox\Temp\Gerard\test\*" -Include *.txt, *.ged
 for ($i=0; $i -lt $files.Count; $i++) 
 { 
   $filename = $files[$i].FullName        
  
  #Rename the file based on strings in the file
   $Account =  (Get-Content -Raw -Path $fileName) 
   $Account -match ":25:.+(\d{10})" 
   $Account = $matches[1]

   $StatementDate  =  (Get-Content -Raw -Path $fileName) 
   $StatementDate -match ":62F:C(?<content>.*)EUR"
   $StatementDate  = $matches['content']

   $file=Get-Item $filename
   $file.Basename 
   $extension=$file.Extension
   
   Rename-Item -Path $filename -NewName "$StatementDate-$Account$extension"
}


Solution

  • You could have achieved similar with the below:

    $Files = Get-ChildItem '/Users/acc/Downloads/bank/*' -Include '*.txt', '*.ged'
    foreach ($File in $Files) {
        $Content = Get-Content -Path $File -Raw
    
        $Account = [Regex]::Match($Content, ':25:.+\*{2}(?<Account>\d{10})\*{2}').Groups['Account'].Value
        $StatementDate = [Regex]::Match($Content, ':62F:C\*{2}(?<StatementDate>\d+)\*{2}EUR').Groups['StatementDate'].Value
    
        Rename-Item -Path $File -NewName ('{0}-{1}{2}' -f $StatementDate, $Account, $File.Extension)
    }