PowerShell根据文本中的字符串重命名文本文件 - >脚本更简洁的方式?

发布于 2025-01-23 07:55:30 字数 1041 浏览 0 评论 0原文

我试图使用帐户号和语句日期在MT940格式中重命名BankStatement。

语句包含以下(示例):

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

我通过组合一些示例编写了以下powershell脚本,但似乎为此目的很长。 问题:有没有简明的方法来编写此脚本?

 $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"
}

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"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2025-01-30 07:55:30

中实现相似

$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)
}
  • 您本可以在以下内容 美学好处,例如能够在集合中轻松访问对象的属性。
    • 例如,通过调用get-item $ fileName仅获取其扩展名来获得文件的对象实例,而是通过使用foreach loop来简化它当前的峰值仍然是system.io.filesysteminfo.fileinfo的对象。因此,我们可以通过访问当前的Itable $ file.extension
    • 来获得其扩展

  • 您正在用get-content多次从文件中读取,每个文件只需要一次。
  • 我认为,使用 .net match() REGEX类的方法比使用-match-Match操作员更干净,但这是个人喜好。
    • 我确实尝试使用 matches() 方法,这样我就可以在一个呼叫中传递两个正则态度模式(在管道上拆分|),但出于某种原因,在两组中,返回,并不是两种模式都匹配了。一个组包含“帐户”的匹配项,而它不适合“语句”,反之亦然。

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)
}
  • By using the foreach loop to iterate over objects in a collection, instead of a for (in-range) loop, you gain some aesthetic benefits like being able to easily access object's properties cleanly in the collection.
    • For example, instead of getting an object instance of your file by calling Get-Item $filename to only get its extension, it is simplified by using the foreach loop and the current iterable is still an object of System.IO.FileSystemInfo.FileInfo. Therefore we can get its extension by accessing the current iterable $File.extension.
  • You were reading from a file multiple times with Get-Content where you only needed to do this once for each file.
  • In my opinion, using the .NET Match() method of the Regex class is cleaner than using the -match operator, but this is personal preference.
    • I did try to use the Matches() method so I could pass both regex patterns (split on a pipe |) in one call, but for some reason, in both groups returned, not both patterns were matched; one group contained a match for 'Account' whereas it did not for 'StatementDate', and vice versa on the other group.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文