在Windows中根据修改日期删除数百万个文件的最有效方法 [英] The most efficient way to delete millions of files based on modified date, in windows
问题描述
目标:使用脚本运行500万至1000万个XML文件并评估其日期(如果时间超过90天,则删除该文件)。该脚本将每天运行。
Goal: Use a script to run through 5 million - 10 million XML files and evaluate their date, if older than 90 days delete the file. The script would be run daily.
问题:使用powershell Get-ChildItem -recurse会导致脚本锁定并无法删除任何文件,我认为这是因为
Problem: Using powershell Get-ChildItem -recurse, causes the script to lock up and fail to delete any files, I assume this is because of the way Get-ChildItem needs to build the whole array before taking any action on any file.
解决方案?:经过大量研究后,我发现[System.IO。目录] :: EnumerateFiles将能够在完全构建数组之前对数组中的项目执行操作,从而使事情更有效率( https://msdn.microsoft.com/library/dd383458%28v=vs.100%29.aspx )。经过更多测试,我发现 foreach($ 1在$ 2中)
比 $ 1 |更有效。 %{}
在我运行此新代码并可能再次使该服务器崩溃之前,是否有人可以提出任何调整建议,以寻求更有效的脚本编写方法?
Solution ?: After lots of research I found that [System.IO.Directory]::EnumerateFiles will be able to take action on items in the array before the array is completely built so that should make things more efficient (https://msdn.microsoft.com/library/dd383458%28v=vs.100%29.aspx). After more testing I found that foreach ($1 in $2)
is more efficient than $1 | % {}
Before I run this new code and potentially crash this server again is there any adjustment anyone can suggest for a more efficient way to script this?
为了进行测试,我刚刚在15,000个目录中创建了15,000 x 0.02KB txt文件,并在其中包含随机数据并运行以下代码,我在 $ date上使用了90秒而不是90天
变量仅用于测试,删除所有txt文件花了6秒钟。
For testing I just created 15,000 x 0.02KB txt files in 15,000 directories with random data in them and ran the below code, I used 90 seconds instead of 90 days on the $date
variable just for the test, it took 6 seconds to delete all the txt files.
$getfiles = [System.IO.Directory]::EnumerateFiles("C:\temp", "*.txt", "AllDirectories")
$date = ([System.DateTime]::Now).AddSeconds(-90)
foreach ($2 in $getfiles) {
if ([System.IO.File]::GetLastWriteTime($2) -le $date) {
[System.IO.File]::Delete($2)
} #if
} #foreach
推荐答案
可处理100,000个文件的Powershell一线式文件,可存储90天以上。
Powershell one-liner that does 100,000 files >= 90 days old.
[IO.Directory]::EnumerateFiles("C:\FOLDER_WITH_FILES_TO_DELETE") |
select -first 100000 | where { [IO.File]::GetLastWriteTime($_) -lt
(Get-Date).AddDays(-90) } | foreach { rm $_ }
或显示进度:
[IO.Directory]::EnumerateFiles("C:\FOLDER_WITH_FILES_TO_DELETE") |
select -first 100000 | where { [IO.File]::GetLastWriteTime($_) -lt
(Get-Date).AddDays(-90) } | foreach { $c = 0 } { Write-Progress
-Activity "Delete Files" -CurrentOperation $_ -PercentComplete
((++$c/100000)*100); rm $_ }
此功能适用于包含大量文件的文件夹。多亏了我的同事道格!
This works on folders that have a very large number of files. Thanks to my co-worker Doug!
这篇关于在Windows中根据修改日期删除数百万个文件的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!