[英]Delete all file and keep the latest using powershell
I have a file that i backup daily, as sample like this :我有一个每天备份的文件,示例如下:
Before deleting all but the latest :在删除除最新之外的所有内容之前:
Folder C:\Backup\
File :
Backup1-Database-A.bak | 1/10/2020 1:24 PM
Backup2-Database-A.bak | 2/10/2020 1:24 PM
Backup1-Database-B.bak | 1/10/2020 1:24 PM
Backup2-Database-B.bak | 2/10/2020 1:24 PM
Backup2-Database-B.bak | 3/10/2020 1:24 PM
Desired state after deleting all but the latest :删除除最新之外的所有内容后所需的状态:
Folder C:\Backup\
File :
Backup2-Database-A.bak | 2/10/2020 1:24 PM
Backup2-Database-B.bak | 3/10/2020 1:24 PM
How can I keep the latest file for each database?如何保留每个数据库的最新文件?
I'm using this script, but it deletes all .bak
so the backup of database A is gone, but I want to keep the latest for each database.我正在使用此脚本,但它删除了所有
.bak
因此数据库 A 的备份消失了,但我想保留每个数据库的最新版本。
$mydays = (Get-Date).AddDays(-7)
$path = "C:\Backup\"
Get-Childitem -path $path -recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "*.bak" -and $_.LastWriteTime -lt $mydays} | Remove-Item -Force
As Lee_Daily suggests, rather than using a time window to filter by, it is simpler to sort your files by last write time and delete all but the latest files.正如Lee_Daily 所建议的那样,与其使用时间窗口进行过滤,不如按上次写入时间对文件进行排序并删除除最新文件之外的所有文件更简单。
The challenge is to determine which files relate to the same database and should therefore be considered as a group , so that you keep the most recent backup in each group .挑战在于确定哪些文件与同一数据库相关,因此应将其视为一个组,以便在每个组中保留最新的备份。
The code below uses the following approach:下面的代码使用以下方法:
The Group-Object
cmdlet allows you to group the input files by the shared part of the file names that indicates the database. Group-Object
cmdlet 允许您按指示数据库的文件名的共享部分对输入文件进行分组。
{ $_.Name -replace '^Backup\\d+-' }
to Group-Object
uses the regex-based -replace
operator to make it group the input file objects by the shared name part that indicates the source database, by removing the 'Backup<n>-'
prefix.{ $_.Name -replace '^Backup\\d+-' }
传递给Group-Object
使用基于正则表达式的-replace
运算符,使其通过指示源数据库的共享名称部分对输入文件对象进行分组,通过删除'Backup<n>-'
前缀。'Backup1-Database-A.bak'
and 'Backup2-Database-A.bak'
is just 'Database-A.bak'
, so the two files are grouped together.'Backup1-Database-A.bak'
和'Backup2-Database-A.bak'
的结果只是'Database-A.bak'
,所以这两个文件被组合在一起。 The files in each resulting group can then be sorted by last write time in descending order, with然后可以按降序对每个结果组中的文件按上次写入时间排序,使用
Sort-Object
. Sort-Object
。
You can exclude the first - and therefore most recent - file from the output with Select-Object
-Skip 1
.您可以使用
Select-Object
-Skip 1
从输出中排除第一个(因此也是最新的)文件。
The resulting files - all but the most recent ones for each database - can then be sent to Remove-Item
.生成的文件 - 除了每个数据库的最新文件之外的所有文件 - 然后可以发送到
Remove-Item
。
$path = 'C:\Backup'
Get-Childitem -Path $path -Recurse -Force -File -Filter *.bak |
Group-Object { $_.Name -replace '^Backup\d+-' } |
ForEach-Object {
$_.Group | Sort-Object LastWriteTime -Descending | Select-Object -Skip 1 |
Remove-Item -Force -Whatif
}
Note: The -WhatIf
common parameter in the command above previews the operation.注意:上面命令中的
-WhatIf
常用参数预览操作。 Remove -WhatIf
once you're sure the operation will do what you want.一旦您确定操作会执行您想要的操作,请删除
-WhatIf
。
Also note the simplification of the initial file selection: -File
(PSv3+) limits output to files only (not also directories), and -Filter *.bak
is an efficient way to filter by filename extension.还要注意的初始文件选择的简化:
-File
(PSv3 +)限制输出到文件只(未也目录),和-Filter *.bak
是由文件扩展名以高效的方式进行筛选。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.