解析脚本永不结束
我有以下脚本,但它永远不会结束执行。解析脚本永不结束
可能是什么问题?我试图调试它,但显然它可以正确使用单个文件,但是当我将它扔到内容充满内容的文件夹失败时。
$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
}
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"
}
}
谢谢,我会在星期一给你一个反馈 – Jackson
经过几次修改后,就像一个魅力。我感谢Toni。 仅供参考, - 未能获得该值,因此我不得不使用-Contain运算符进行功能匹配。 – Jackson
好的,为了使'-in'工作,右侧必须是数组,并且左侧必须是逐字包含在该数组中。如果你用一个简单的'Get-Content'获得一个文件的功能列表,其中每个功能都是一行(尽管要注意多余的空格),那么应该是这种情况。 '-contains'操作符完全相同,只有左侧和右侧交换(数组左侧,搜索值右侧)。所以我对你如何解决这个问题有点困惑,但只要它能够工作...... – TToni
呀,为什么不...... – Jackson
你有一些SAMP我们可以测试的文件?您的disabledFeatures和scrubbed源代码文件?你有没有尝试在文件之间放置一些输出行,让你知道它正在继续?你如何衡量循环缺乏进展?如果有更好的方法,很高兴知道这段代码应该做什么。 – Matt
您是否使用断点在PowerShell ISE中进行调试?您的代码可能会很慢,部分原因是您在每次迭代中都重新读取txt文件,并在默认模式下使用Get-Content,对于大量文件有很多行,速度非常慢。 – wOxxOm