英文:
Delete Zip Folders in a Specific Directory when there are more than 4 in the directory using CMD Line Script
问题
我有一个每天备份数据库并压缩文件存储在本地驱动器上的脚本。我想要做的是,一旦目录中有4个以上的ZIP文件,就删除最旧的ZIP文件。目录是D:\Temp\Backup。我继承了这个脚本,对命令行不太熟悉。如果能帮忙,将不胜感激,因为我不知道从哪里开始。由于脚本的敏感性,我不能分享现有的代码。
谢谢您提前。
英文:
I have a script that backs up a database daily and zips up the files and stores it on a local drive. What I would like to do is then delete the oldest zip folder once there are more than 4 in the directory. The directory is D:\Temp\Backup. I inherited this script and am not skilled in command line at all. Any help with this would be great as I am not sure where to start. Due to the sensitivity of the script, I cannot share the existing code.
Thank you in advance.
答案1
得分: 0
我曾经写过类似的东西。
它检查文件夹中的文件是否超过4天,并删除所有旧文件,将文件名写入名为"deletedlog.txt"的文本文件中。
$Folder="D:\Temp\Backup"
Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-4)} |
ForEach-Object {
$_ | del -Force
$_.FullName | Out-File $Folder\deletedlog.txt -Append
}
Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {$_.PsIsContainer -eq $True} |
? {$_.getfiles().count -eq 0} |
ForEach-Object {
$_ | del -Force
$_.FullName | Out-File $Folder\deletedlog.txt -Append
}
英文:
I wrote onc sth like that.
It checks the folder for files older than 4 days, deletes all tfiles older and writes the filename into a textfile called "deletedlog.txt"
$Folder="D:\Temp\Backup"
Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-4)} |
ForEach-Object {
$_ | del -Force
$_.FullName | Out-File $Folder\deletedlog.txt -Append
}
Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {$_.PsIsContainer -eq $True} |
? {$_.getfiles().count -eq 0} |
ForEach-Object {
$_ | del -Force
$_.FullName | Out-File $Folder\deletedlog.txt -Append
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论