循环过早停止

时间:2016-06-30 07:44:42

标签: powershell

我正在编写一个查看目录树的应用程序,并根据上次写入时间和只读属性报告文件夹是否处于非活动状态。

然而,即使有数千个文件夹,我的循环也会在7次迭代后停止。

我的代码如下:

let line = d3.svg.line()
    .interpolate(interpolationType)
    .x((d) => { return xScale(new Date(d.to_timestamp)); })
    .y((d) => { return yScale(d.count); });

如果我在Foreach循环中注释掉data.forEach(d => d.to_timestamp = new Date(d.to_timestamp)); 函数调用,则会打印所有文件夹,但是在函数调用后,它会在几次迭代后停止。发生了什么事?

1 个答案:

答案 0 :(得分:2)

您无法将continueForeach-Object cmdlet一起使用。 Foreach-Object是一个cmdlet,不是循环。你想要使用循环:

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})   
}