1

我们有一个每隔一天运行一次的备份,但是文件很大,一旦我们获得一定数量的带有文件签名的备份文件,我们就想删除每隔一个。

我试过这个:

$Drive = "E:\temp\"
$deleteTime = -42;
$limit = (Get-Date).AddDays($deleteTime)

#this is finding the correct files but I don't think it's really in an array
$temp1 = Get-ChildItem -Path $Drive -filter "*junk.vhd*" | Where-Object {$_.LastWriteTime -lt $limit} | Select -Expand Name

for($i=$temp1.GetLowerBound(0); $i -le $temp1.GetUpperBound(0); $i+=2) {

   Write-Host "removing $temp1[$i]"  #this is listing the entire array with a [0] for first one and the third [2] element also, whether I cast to an array or not

}

我目前尝试了这个而不是上面的 (Get-ChildItem) 行,但它列出了 [0] 的整个垃圾文件集,而不仅仅是 [0] 处的第一个 junk.vhd:

[array]$temp1 =@( Get-ChildItem -Path $Drive -filter "*junk.vhd*" | Where-Object {$_.LastWriteTime -lt $limit} | Foreach-Object {$_.Name} )

我也试过这个:

$limit = (Get-Date).AddDays(-42)
$list = (dir -Filter *junk.ps1 | where LastWriteTime -lt $limit).FullName
$count = $list.Length
for ($i = 0; $i -lt $count; $i += 2)
{
    Write-Verbose "[$i] $($list[$i])"
    #it's not getting in here because I'm not sure how 
    #to add the $Drive location and list is empty
}

有没有人建议如何从 $Drive 位置获取带有签名 *junk.vhd 的文件名数组,以便我可以遍历它们并删除所有其他文件名?

互联网搜索并没有出现太多。

4

1 回答 1

0

这对我有用:

$deleteTime = -12;
$limit = (Get-Date).AddDays($deleteTime)
$t = Get-ChildItem -Path $pwd -filter "p*.txt" | Where-Object {$_.LastWriteTime -lt $limit} | Select -Expand Name  

foreach ($a in $t) { Write-Host "Name : $a" }     

我从你正在寻找的东西中错过了什么?
(显然,您需要维护一个计数器并在 foreach() 语句的主体中进行一些模运算......)

这也有效:

for($i=$t.GetLowerBound(0); $i -le $t.GetUpperBound(0); $i+=2) {  
   $n = $t[$i]  
   Write-Host "removing $n"    
}  
于 2014-08-07T17:12:57.520 回答