我正在嘗試讀取文本文件的以下腳本,然後使用收集的數據運行算法。該算法必須應用於當前文件夾中的每個文件,因此有2個主循環。從根文件夾解析文件的PowerShell腳本
循環1用於文件夾中的文件。
循環2用於算法中使用的文本行條目。
循環3用於算法本身。
#$path = (Get-Item -Path ".\" -Verbose).FullName
$path = split-path -parent $MyInvocation.MyCommand.Definition
$files = Get-ChildItem "$path\test" -r # root path $PSScriptRoot
#echo $path
#echo $files
#echo $files.Count
ForEach ($file in $files){
echo "the value of i is" $i
#echo $file.FullName
#iterate through files from the current folder.
$data = Get-Content -Path $files.FullName
#echo "$data"
# parse DisabledFeatures.txt file as array of strings (1 string per line of the file)
$feature = Get-Content "$path\Disabled_Features.txt"
#echo $feature.Count
#iterate for each string entry in $feature array (read from txt file)
for($counter=0; $counter -lt $feature.Count; $counter++){
#retrieve array value to use it in the main algorythm
$groupID = $feature[$counter]
echo $groupID
$data | ForEach-Object -Begin { $ignore = $false; $levels = 0 } -Process {
#Start ignoring text after we've found the trigger
if($_ -match "^#ifdef $groupID") {
$ignore = $true
ECHO "TRUE ifdef feature"
}
#Track nested groups
elseif($ignore) {
if ($_ -match '^#ifdef') {
$levels++
echo "levels++"
}
elseif ($_ -match '#endif') {
if($levels -ge 1) { $levels-- }
#If no nesting, we've hit the end of our targeted group. Stop ignoring
else { $ignore = $false }
echo "stop ignoring"
}
}
#Write line
else { $_ }
echo "write line"
}
}
}
編輯:更新腳本。
'$ I -lt'和'$計數器-lt' – TessellatingHeckler
「這似乎並沒有工作」 .. *如何?*你期望什麼結果?你實際得到了什麼結果? –
更新了腳本。它旨在刪除使用#ifdef特性進行starifying的段落,並以相應的#endif結尾。所以#ifdef #endif之間的段落不關心 – Jackson