解析脚本永远不会结束

时间:2017-02-17 13:08:15

标签: powershell parsing infinite-loop

我有以下脚本,但它永远不会结束执行。

可能是什么问题?我尝试调试它,但显然它可以正常使用单个文件,但是当我把它扔到一个充满内容的文件夹失败时。

$path = split-path -parent $MyInvocation.MyCommand.Definition
$files = Get-ChildItem "$path\CodeForCertification\5_SourceCode\*" -Include *.c,*.h -Recurse | where{ 
        ! $_.PSIsContainer 
}#$PSScriptRoot

ForEach ($file in $files){

    $data = Get-Content -Path $file.FullName
    $feature = Get-Content "$path\Disabled_Features.txt"    
    #[System.ArrayList]$Modifier
    $nl=[Environment]::NewLine
    $Modifier=@()
    $flag=0
    $data = $data | ForEach-Object -Begin {
        $ignore = $false; $levels = 0 
    } -Process {
        for($counter=0; $counter -lt $feature.Count; $counter++){
            $parse = $feature[$counter]
            if($_ -match "^#ifdef $parse" -And $flag -eq '0') {
                $ignore = $true
                $flag = 1;
            }
        }
        if($ignore) {
            if ($_ -match "^#ifdef") {
                $levels++ 
            }elseif ($_ -match "#endif") {
                if($levels -ge 1) {
                    $levels--
                    if($levels -eq '0'){
                        $ignore = $false
                    }                        
                }
            }
        }else {
            $flag=0
            $temp=$_
            $_
            $Modifier+="$temp"
        }
    }
    $data | Out-File $file.FullName
}

1 个答案:

答案 0 :(得分:1)

好的,杰克逊,在您输入某种问题垃圾邮件过滤器之前让我们解决您的问题; - )

考虑一下(只是把它放在脚本开头的某个地方):

function RemoveUndesiredFeatures([string[]]$lines,[string[]]$undesiredFeatures)
{    
    $inIgnoreBlock = $false
    $nestingLevel = 0

    foreach ($line in $lines)
    {
        if ($inIgnoreBlock)
        {
            # Only search for nested blocks and end of block
            if ($line -like "#ifdef*")
            {
                $nestingLevel++
            }
            elseif ($line -like "#endif*")
            {
                $nestingLevel--
            }

            if ($nestingLevel -eq 0)
            {
                $inIgnoreBlock = $false
            }
        }
        else
        {
            # Search for undesired feature
            $isIfdefMatch = $line -match "#ifdef (?<feature>\w+)"

            if ($isIfdefMatch -and ($Matches.feature -in $undesiredFeatures))
            {
                # Ignore Feature
                $inIgnoreBlock = $true
                $nestingLevel++
            }
            else
            {
                # Output line
                $line
            }
        }    
    }
}

以下是我使用它的示例:

$undesiredFeatures = @("F1","F2") # Just as example. Get-Content on a file with features is also fine

$files = Get-ChildItem *.c,*.h -Recurse # Again, just as example

foreach ($file in $files)
{
    $lines = Get-Content $file.FullName

    $changedLines = RemoveUndesiredFeatures $lines $undesiredFeatures

    if ($changedLines.Count -ne $lines.Count)
    {
        # Features were removed. Write out changed file (to a different file to preserve my test files)
        Set-Content -Value $changedLines -Path "$($file.FullName).changed"
    }
}