[英]Execute get-childitem, but iterate in reverse order?
我有一个包含500,00+个文件的文件夹。 我正在尝试遍历此文件夹并运行一些逻辑来确定是否可以删除不需要的文件。 问题是这个过程需要半定期运行,并且需要删除的新文件当前位于列表的末尾。
我把以下代码列表放在一起来对它进行排序:
gci $RPT | %{
$flag = 0;
$number = [int]($_.Name | select-string -pattern "\d{12}" -Allmatches).Matches.Value
if ($submidlist -match "^$number$"){
if ($_ -notmatch "acct\.csv|jpd\.csv|jss\.pdf|jman\.pdf|3600\.pdf|cont\.pdf|msl\.txt|pres\.pdf|tray\.pdf|qual\.pdf|zipl\.pdf"){
echo "DELETE SUBMID $_"
remove-item $RPT\$_
$count++
$totalcount++
$flag = 1;
}
}
if ($jobidlist -match "^$number$"){
if ($_ -match "acct\.csv|jpd\.csv|jss\.pdf|jman\.pdf|3600\.pdf|cont\.pdf|msl\.txt|pres\.pdf|tray\.pdf|qual\.pdf|zipl\.pdf"){
echo "DELETE JOBID $_"
remove-item $RPT\$_
$count++
$totalcount++
$flag = 1;
}
}
}
目前,运行上述脚本需要24个小时,但仍然没有到达列表的末尾。 有没有办法优化这个或反转get-childitem迭代这个文件夹的顺序?
function Delete-Items($List, [string]$ListName){
$DoNotDelete = @("acct.csv","jpd.csv","jss.pdf","jman.pdf","3600.pdf","cont.pdf","msl.txt","pres.pdf","tray.pdf","qual.pdf","zipl.pdf")
$List = $List | %{
"*$_*"
}
Get-ChildItem C:\TEST\56381643\ -Recurse -Include $List -Directory | %{
Get-ChildItem $_.FullName -Exclude $DoNotDelete -Recurse | %{
echo "DELETE $ListName $($_.name | select-string -pattern "\d{12}")"
Remove-Item -Path $_.FullName -WhatIf
}
}
}
#Example Usage
$JobList = @(
098765432109
123456789012
)
$SubmitList = @(
234567890123
)
Delete-Items -List $JobList -ListName JOBID
Delete-Items -List $SubmitList -ListName SUBMID
让我们回顾一下函数中发生的最新情况。
我们有一系列不能删除的文件
我们通过在数组中的每个项目之前和之后添加*
来将$list
数字转换为通配符。 然后,我们只搜索那些包含这些数字的目录。
然后我们使用另一个Get-ChildItem to get the files in each directory but exclude the ones mentioned in
$ DoNotDelete`中Get-ChildItem to get the files in each directory but exclude the ones mentioned in
文件。
如果要删除文件,请删除remove-item
上的-Whatif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.