【发布时间】:2014-02-19 09:06:24
【问题描述】:
我遇到了一个小问题,场景是这样的:13.300 个文件夹,每个文件夹中有 20 个不同内容的 .zip 文件。我需要找到其中包含 .txt 文件的文件,然后将其删除,但在 13.300 个文件夹中保留最新的 X。
但首先我想检查一下我总共有多少个包含 .txt 文件的 zip 文件。
脚本如下所示:
# the .zip files are in the 13.300 folders which are in "c:\topfolder"
$motherfolder = "c:\topfolder"
$childfolder = Get-Childitem "c:\topfolder"
$searchTerms = @( ".txt")
function openZip($zipFile){
try{
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" ) | Out-Null;
$zipContens = [System.IO.Compression.ZipFile]::OpenRead($zipFile);
$zipContens.Entries | % {
foreach($searchTerm in $searchTerms){
if ($_.Name -imatch $searchTerm){
Write-Output ($_.Name + "," + $_.FullName + "," + $_.CompressedLength + "," + $_.LastWriteTime + "," + $_.Length);
}
}
}
}
catch{
Write-Output ("There was an error:" + $_.Exception.Message);
}
}
foreach ($childfolder in $motherfolder) {
Get-ChildItem $zipfolder -recurse *.zip | sort modifyTime -desc | select -skip 2 {
openZip $_.FullName >> "C:\Projects\PSScripts\report.csv"
}
}
当这个脚本在服务器上运行时,它需要很长时间并吃掉所有的内存,那么修改它的最佳方法是什么?首先让脚本只检查前 500 个文件夹而不是 13.300 ?而不是另外500个等等? 第一次遇到这个问题……
【问题讨论】:
-
您可以使用其他软件(例如 7Zip)来查看 zip 文件吗?普通的 .Net 是必需的吗?
-
是的,我们可以使用 7zip,这不是问题。我们在那台机器上有 .net 4.5,这就是我采用这种方法的原因。
标签: powershell zip out-of-memory powershell-3.0