0

I am working on following script which tries to read a text file, and then run an algorithm with the collected data. The algorithm must be applied to each file in the current folder, so there are 2 main loops.

Loop 1 is for files in folder.

Loop 2 is for the text line entries used in the algorithm.

Loop 3 is for the algorithm itself.

#$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"
        }  
    }
}

Edit: Updated script.

3
  • $i -lt and $counter -lt Commented Feb 16, 2017 at 9:20
  • 1
    "It does not seem to work" ... how? What result do you expect? What result do you actually get? Commented Feb 16, 2017 at 10:24
  • updated the script. it is intended to delete paragraphs stariting with #ifdef feature and ending with the correspondant #endif. So #ifdef #endif paragraphs in between don't care Commented Feb 16, 2017 at 10:32

1 Answer 1

1

Your script loops multiple times (once for each disabled feature) through each file content ($data), but you just write the result of the filter operation to the output without modifying $data.

So the next feature loop runs across the same, unmodified $data content. If the last disabled feature does not happen to be in the file, the last result returns the file content unchanged.

You need to capture the output of the innermost loop into a new string array, then assign that to $data before you run the next feature loop. You can then write that last $data to your desired output.

Minor improvement for readability: Use a foreach loop for the features, too, like

foreach ($groupID in $feature)
{
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you provide an example about how should I pipe the captured string to $Data? I guess a simple way to capture it is by appending last $_ in string array? Thanks a lot !
Replace $data | ForEach-Object with $data = $data | ForEach-Object to capture the filtered output. You have to get rid of the echo statements though or change them to Write-Host
Thanks, that's exactly what I needed !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.