1

1 日おきに実行するバックアップがありますが、ファイルが大きいため、ファイル シグネチャで一定量のバックアップ ファイルを取得したら、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] の最初の jump.vhd だけでなく、[0] のジャンク ファイルのセット全体が一覧表示されました。

[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 に答える