PowerShell 为这类事情提供了易于使用的 cmdlet。
我的问题是您是否希望使用文件名中的日期,或者文件本身的实际 LastWriteTime 日期(如文件资源管理器中所示)。
以下两种处理方式。我已经放了很多代码 cmets 来帮助您了解图片。
如果您想根据文件的实际上次写入时间删除文件:
$sourceFolder = 'D:\test' # put the path to the folder where your files are here
$filePrefix = 'constant_filename_prefix'
Get-ChildItem -Path $sourceFolder -Filter "$filePrefix*.zip" -File | # get files that start with the prefix and have the extension '.zip'
Where-Object { $_.BaseName -match '_\d{14}$' } | # that end with an underscore followed by 14 digits
Sort-Object -Property LastWriteTime -Descending | # sort on the LastWriteTime property
Select-Object -Skip 1 | # select them all except for the first (most recent) one
Remove-Item -Force -WhatIf # delete these files
或
如果您想根据文件名中的日期删除文件。
因为您使用的日期格式是可排序的,所以您可以安全地对文件 BaseName 的最后 14 位进行排序:
$sourceFolder = 'D:\test'
$filePrefix = 'constant_filename_prefix'
Get-ChildItem -Path $sourceFolder -Filter "$filePrefix*.zip" -File | # get files that start with the prefix and have the extension '.zip'
Where-Object { $_.BaseName -match '_\d{14}$' } | # that end with an underscore followed by 14 digits
Sort-Object -Property @{Expression = {$_.BaseName.Substring(14)}} -Descending | # sort on the last 14 digits descending
Select-Object -Skip 1 | # select them all except for the first (most recent) one
Remove-Item -Force -WhatIf # delete these files
在这两种选择中,您都会发现-WhatIf 的末尾有一个开关Remove-Item cmdlet。 Yhis 用于测试代码,实际上不会删除任何文件。相反,使用此开关,它会在控制台中写出会发生什么。
一旦您对此输出感到满意,您可以删除或注释掉 -WhatIf 开关,让代码删除文件。
更新
据我所知,该文件夹中有几天的多个文件,您希望每天保留最新的文件,删除其他文件。
在这种情况下,我们必须创建文件的“日”组,并按日期对每个组进行排序并删除旧文件。
这就是Group-Object 的用武之地。
方法 1) 使用文件的 LastWriteTime 属性
$sourceFolder = 'D:\test' # put the path to the folder where your files are here
$filePrefix = 'constant_filename_prefix'
Get-ChildItem -Path $sourceFolder -Filter "$filePrefix*.zip" -File | # get files that start with the prefix and have the extension '.zip'
Where-Object { $_.BaseName -match '_\d{14}$' } | # that end with an underscore followed by 14 digits
Group-Object -Property @{Expression = { $_.LastWriteTime.Date }} | # create groups based on the date part without time part
ForEach-Object {
$_.Group |
Sort-Object -Property LastWriteTime -Descending | # sort on the LastWriteTime property
Select-Object -Skip 1 | # select them all except for the first (most recent) one
Remove-Item -Force -WhatIf # delete these files
}
方法 2) 使用取自文件名的日期:
$sourceFolder = 'D:\test' # put the path to the folder where your files are here
$filePrefix = 'constant_filename_prefix'
Get-ChildItem -Path $sourceFolder -Filter "$filePrefix*.zip" -File | # get files that start with the prefix and have the extension '.zip'
Where-Object { $_.BaseName -match '_\d{14}$' } | # that end with an underscore followed by 14 digits
Group-Object -Property @{Expression = { ($_.BaseName -split '_')[-1].Substring(0,8)}} | # create groups based on the date part without time part
ForEach-Object {
$_.Group |
Sort-Object -Property @{Expression = {$_.BaseName.Substring(14)}} -Descending | # sort on the last 14 digits descending
Select-Object -Skip 1 | # select them all except for the first (most recent) one
Remove-Item -Force -WhatIf # delete these files
}