PowerShell根据文本中的字符串重命名文本文件 - >脚本更简洁的方式?
我试图使用帐户号和语句日期在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
中实现相似
get-item $ fileName
仅获取其扩展名来获得文件的对象实例,而是通过使用foreach
loop来简化它当前的峰值仍然是system.io.filesysteminfo.fileinfo
的对象。因此,我们可以通过访问当前的Itable$ file.extension
。来获得其扩展
get-content
多次从文件中读取,每个文件只需要一次。match()
REGEX类的方法比使用-match-Match
操作员更干净,但这是个人喜好。matches()
方法,这样我就可以在一个呼叫中传递两个正则态度模式(在管道上拆分|
),但出于某种原因,在两组中,返回,并不是两种模式都匹配了。一个组包含“帐户”的匹配项,而它不适合“语句”,反之亦然。You could have achieved similar with the below:
foreach
loop to iterate over objects in a collection, instead of afor
(in-range) loop, you gain some aesthetic benefits like being able to easily access object's properties cleanly in the collection.Get-Item $filename
to only get its extension, it is simplified by using theforeach
loop and the current iterable is still an object ofSystem.IO.FileSystemInfo.FileInfo
. Therefore we can get its extension by accessing the current iterable$File.extension
.Get-Content
where you only needed to do this once for each file.Match()
method of the Regex class is cleaner than using the-match
operator, but this is personal preference.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.