vendredi 12 juillet 2019

Comparison of strings not returning correct information

We're working with a text file that contains many different types of reports. Some of those reports need to either have some words changed or just copy them over exactly as they are.

The file has to stay a single text file, so the idea is to move through the file, comparing the lines. If a line is found that is a "ReportType1", then we need to change some wording, so we go into an inner loop, extracting the data and changing words as we go. The loop ends when it reaches a footer in the report and should move on to the next report.

We've tried -match, -like, -contains, -eq, but it never works quite like it's supposed to. We either get data that's been changed/reformatted that shouldn't be or we're only getting the header data.

Add-Type -AssemblyName System.Collections
Add-Type -AssemblyName System.Text.RegularExpressions

[System.Collections.Generic.List[string]]$content = @()

$inputFile   = "drive\folder\inputfile.txt"
$outputFile  = "drive\folder\outputfile.txt"

#This will retrieve the total number of lines in the file
$FileContent = Get-Content $inputFile
$FileLineCount = $FileContent | Measure-Object -Line
$TotalLines = $FileContent.Count

$TotalLines++ #Need to increase by one; the last line is blank

$startLine   = 0
$lineCounter = 0

#Start reading the file; this is the Header section
#Number of lines may vary, but data is copied over word
#for word
foreach($line in Get-Content $inputfile)
{
    $startLine++
    If($line -match "FOOTER")
    {
        [void]$content.Add( $line )
        break
    }
    else
    {
        [void]$content.Add( $line )
    }
}
## ^^This section works perfectly

#Start reading the body of the file
Do {
    #Start reading from the current position
    #This should change with each report read
    $line = Get-Content $inputFile | select -Skip $startLine

    If($line -match "ReportType1") #If it's a ReportType1, some wording needs to be changed
    {
        #Start reading the file from the current position
        #Should loop through this report only
        foreach($line in Get-Content $inputFile | select -skip $startline) 
        {
            If($line -match "FOOTER") #End of the current record
            {
                [void]$content.Add( $line )
                break #break out of the loop and continue reading the file from the new current position
            }
            elseif ($line -match "OldWord") #Have to replace a word on some lines
            {
                $line = $line.Replace("OldWord","NewWord")
                [void]$content.Add( $line ) 
            }
            else
            { 
                [void]$content.Add( $line ) 
            }
            $startline++                
        }
    }
    else
    {
         If($line -match "ReportType2") #ReportType2 can just be copied over line for line
         {
             #Start reading the file from the current position
             #Should loop through this report only
             foreach($line in Get-Content $inputFile | select -skip $startline) 
             {
                If($line -match "FOOTER") #End of the current record
                {
                    [void]$content.Add( $line )
                    break #break out of the loop and continue reading the file from the new current position
                }
                else
                { 
                    [void]$content.Add( $line ) 
                }
                $startline++                
        }
    }
    $startline++
} until ($startline -eq $TotalLines)

[System.IO.File]::WriteAllLines( $outputFile, $content ) | Out-Null

It sort of works, but we're getting some unexpected behavior. The reports look fine and all, but it's changing words in "ReportType2", even though the code isn't set up to do that. It's like it's only going through the first IF statement. But how can it be if the lines don't match up?

We know the $startline variable is increasing through the iterations, so it's not like it's stuck on one line. However, doing 'Write-Host' shows $line is always "ReportType1", which can't be true because the lines are showing up in the reports like they're supposed to be.

All we can figure is we're missing something, probably pretty obvious, that will get this to output the data correctly.

Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire