[英]Powershell: Get-ChildItem, get latest file of files with similar names
I got a script to get files from a folder that I then put in a HTML-Table and mail.我有一个脚本可以从文件夹中获取文件,然后将其放入 HTML 表格和邮件中。 In the folder you have files like
HR May 2020
, HR April 2020
, RR May 2021
etc.在文件夹中,您有
HR May 2020
、 HR April 2020
、 RR May 2021
等文件。
Below is the code itself as a sample, this looks for other files but they come every month as well.下面是作为示例的代码本身,它会查找其他文件,但它们也每个月都会出现。 In total I will filter 8 files.
总共我将过滤 8 个文件。
Get-ChildItem -Path D:\Temp\Test |
Where-Object { $_.Name -match '^RR_Prognos.*|^AllokeringBogNycklar.*' } |
Sort-Object -Property LastWriteTime |
Select-Object LastWriteTime,FullName
Now I am only interested in seeing the latest file of each, so using last or -days, month, hours or similar wont work.现在我只对查看每个文件的最新文件感兴趣,因此使用 last 或 -days、month、hours 或类似的方法是行不通的。
I tried to find a better solution googling it but could not come up with anything that solved the problem.我试图找到一个更好的解决方案来谷歌搜索它,但无法想出任何解决问题的方法。
So I just need to add to the code it it picks the lastest of each file i filter on, the filter is so it does not care about the month in the name.所以我只需要添加到代码中,它会选择我过滤的每个文件的最新文件,过滤器是这样的,所以它不关心名称中的月份。
Edit: Lets say I would use:编辑:可以说我会使用:
Get-ChildItem -Path c:\tm1 | Where-Object { $_.Name -match '^RR_Prognos.*|^AllokeringBogNycklar.*|^Aktivversion.*|^AllokeringNycklar.*|^HR_prognos.*|^KostnaderDK.*|KostnaderProdukt_prognos.*|^Parametrar_prognos.*|ProduktNyckel_prognos apr.*' } | Sort-Object -Property LastWriteTime -Descending | Select-First 8 | Select-Object LastWriteTime,FullName
Then if one file does not come with the batch, it would show the 2nd last one of that as well.然后,如果批处理中没有一个文件,它也会显示第二个最后一个文件。 Is there a easier way to block that from happening?
有没有更简单的方法来阻止这种情况的发生?
Ok, now that you've provided the whole list that you filter against I can write up a real answer.好的,现在您已经提供了您过滤的整个列表,我可以写一个真正的答案。 Here we'll group by file name, then sort each group and grab the last one from each group:
在这里,我们将按文件名分组,然后对每个组进行排序并从每个组中获取最后一个:
Get-ChildItem -Path c:\tm1 |
Where-Object { $_.Name -match '^RR_Prognos.*|^AllokeringBogNycklar.*|^Aktivversion.*|^AllokeringNycklar.*|^HR_prognos.*|^KostnaderDK.*|KostnaderProdukt_prognos.*|^Parametrar_prognos.*|ProduktNyckel_prognos apr.*' } |
Group {$_.Name -replace '.*?(^RR_Prognos|^AllokeringBogNycklar|^Aktivversion|^AllokeringNycklar|^HR_prognos|^KostnaderDK|KostnaderProdukt_prognos|^Parametrar_prognos|ProduktNyckel_prognos apr).*','$1'} |
ForEach-Object {
$_.Group |
Sort-Object -Property LastWriteTime -Descending |
Select -First 1
} |
Select-Object LastWriteTime,FullName
This might be what you are looking for.这可能是您正在寻找的。 This iterates over each search string where you need the newest file.
这将遍历您需要最新文件的每个搜索字符串。
$searchStrings = @('^pattern1*', '^pattern2*', '^pattern3*')
foreach($searchString in $searchStrings) {
$items = Get-ChildItem -Path $folder | Where-Object { $_.Name -match "$searchString" } | Sort-Object -Property LastWriteTime -Descending
$newestItem = $items[0]
Write-Host "newest item for '$searchString' is $newestItem"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.