2017-02-17 68 views
0

我有以下腳本,但它永遠不會結束執行。解析腳本永不結束

可能是什麼問題?我試圖調試它,但顯然它可以正確使用單個文件,但是當我將它扔到內容充滿內容的文件夾失敗時。

$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 
    [email protected]() 
    $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 
} 
+0

呀,爲什麼不...... – Jackson

+0

你有一些SAMP我們可以測試的文件?您的disabledFeatures和scrubbed源代碼文件?你有沒有嘗試在文件之間放置一些輸出行,讓你知道它正在繼續?你如何衡量循環缺乏進展?如果有更好的方法,很高興知道這段代碼應該做什麼。 – Matt

+0

您是否使用斷點在PowerShell ISE中進行調試?您的代碼可能會很慢,部分原因是您在每次迭代中都重新讀取txt文件,並在默認模式下使用Get-Content,對於大量文件有很多行,速度非常慢。 – wOxxOm

回答

1

OK,傑克遜,讓我們來解決你的問題,你進入了某種問題的垃圾郵件過濾;-)的

考慮這個(只是把它放在你的腳本的開始)前:

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" 
    } 
} 
+0

謝謝,我會在星期一給你一個反饋 – Jackson

+0

經過幾次修改後,就像一個魅力。我感謝Toni。 僅供參考, - 未能獲得該值,因此我不得不使用-Co​​ntain運算符進行功能匹配。 – Jackson

+0

好的,爲了使'-in'工作,右側必須是數組,並且左側必須是逐字包含在該數組中。如果你用一個簡單的'Get-Content'獲得一個文件的功能列表,其中每個功能都是一行(儘管要注意多餘的空格),那麼應該是這種情況。 '-contains'操作符完全相同,只有左側和右側交換(數組左側,搜索值右側)。所以我對你如何解決這個問題有點困惑,但只要它能夠工作...... – TToni