【发布时间】:2015-07-30 17:05:24
【问题描述】:
我编写了一个 powershell 脚本,它将遍历三个不同的路径并获取少于 7 年的文件列表,并将它们从当前时间戳中删除。 我正在获得文件的创建年份,如果能够递归地遍历所有这三个路径。 问题出在 3 个中,两个路径的文件夹和文件太多,因此当脚本在循环中时,它会显示内存异常。我也无法设置 maxmemorypershellMB,因为我没有访问权限。
我可以做的任何其他事情以避免内存异常 这是下面的一段代码:
$files = Get-ChildItem "$path" –Recurse -file
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].FullName #file name
$FileDate = (Get-ChildItem $outfile).CreationTime #get creation date of file
$creationYear = $FileDate.Year
$creationMonth =$FileDate.Month #get only year out of creation date
If( $creationYear -lt $purgeYear ){
If (Test-Path $outfile){ #check if file exist then only proceed
$text=[string]$creationYear+" "+$outfile
$text >> 'listOfFilesToBeDeleted_PROD.txt' #this will get list of files to be deleted
#remove-item $outfile
}
}
}
【问题讨论】:
-
您可以将
Get-ChildItem "$path" -Recurse -file按一级文件夹拆分,这样您的$files的成员将始终少于完整集,并且其先前的集将被丢弃并被GC'd,允许您解析每个路径有更多文件。 -
我是 powershell 新手,不知道如何将它们限制在一个级别
-
还有一级目录太多,每个目录包含的文件太多。相同的脚本在本地和 QA 上运行良好
标签: powershell