2016-06-30 8 views
2

私はディレクトリツリーを調べ、最後の書き込み時間と読み取り専用属性に基づいてフォルダが非アクティブであることを報告するアプリケーションを作成しています。ループが途中で止まる

しかし、何千ものフォルダがあっても7回の繰り返しの後に私のループは停止します。

私のコードは次のようになります。私はそれがすべてのフォルダを表示しますforeachループでFolderInactive関数呼び出しをコメントアウトした場合

function FolderInactive{ 
    Param([string]$Path) 
    $date = (Get-Date).AddDays(-365) 
    $anyReadOnly = $false 
    Get-ChildItem $Path -File -ErrorAction SilentlyContinue | ForEach-Object { 
     if($_.LastWriteTime -ge $date){ 
      $false 
      continue 

     } 
     if($_.IsReadOnly -eq $false){ 
      $anyReadOnly = $true 
     } 
    } 
    $anyReadOnly 
} 

Get-ChildItem "some drive" -Recurse | where {$_.PSIsContainer} | Foreach-Object { 
    Write-Host $_.FullName 
    FolderInactive($_.FullName) 

} 

が、関数では、それは数回の反復後に停止を呼び出します。何が起こっている?

答えて

2

Foreach-Objectコマンドレットにcontinueを使用することはできません。 Foreach-Objectはコマンドレットですループではありません

function FolderInactive{ 
    Param([string]$Path) 
    $date = (Get-Date).AddDays(-365) 
    $anyReadOnly = $false 
    $items = Get-ChildItem $Path -File -ErrorAction SilentlyContinue 
    foreach($item in $items) 
    { 
     if($item.LastWriteTime -ge $date){ 
      $false 
      continue 

     } 
     if($item.IsReadOnly -eq $false){ 
      $anyReadOnly = $true 
     } 
    } 
    $anyReadOnly 
} 

これも単純化することができる:

function FolderInactive 
{ 
    Param([string]$Path) 
    $date = (Get-Date).AddYears(-1) 
    $null -ne (Get-ChildItem $Path -File -ErrorAction SilentlyContinue | 
     Where {$_.LastWriteTime -ge $date -and $_.IsReadOnly}) 
} 
かわりに、ループを使用したいです
関連する問題