Search code examples
powershellappendprepend

Powershell add header record


I have a process in SSIS where I create three files.

Header.txt work.txt Trailer.txt

Then I use an Execute Process Task to call my Powershell script. I basically need to take the work.txt file and prepend the header record to it (while maintaining integrity of original values in work.txt) and then append the trailer record (which is generated with total row counts, etc.).

Currently I have:

Set-Location "H:\Documentation\Projects\CVS\StageCVS"
Clear-Content "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility"
Get-Content Header.txt, work.txt, Trailer.txt|out-file "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility" -Confirm

This is fine in testing where I only had 1000 rows, but now that I have 67,000 rows the process takes forever.

I was looking at the Add-Content cmdlet but I can't find an example where it adds the header. Can someone assist with the syntax on going to the first line in the file and then adding the content before that first line?

many thanks in advance!

Just to clarify: I would like to build off the work.txt file. This si where the majority of the data is already, so instead of rewriting it all to a new file, I think a copy would make more sense. So in theory I would create all three files. copy the work file to say workfile.txt . Prepend header to workfile, append trailer to workfile, rename workfile.

UPDATE This seems to work for the trailer.

Set-Location "H:\Documentation\Projects\CVS\StageCVS"
#Clear-Content "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility"
Copy-Item work.txt workfile.txt
#Get-Content Header.txt, work.txt, Trailer.txt|out-file "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility"
Add-Content workfile.txt -value (get-content Trailer.txt)

UPDATE

Also tried:

Set-Location "H:\Documentation\Projects\CVS\StageCVS"
$header = "H:\Documentation\Projects\CVS\StageCVS\Header.txt"
#Clear-Content "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility.txt"
Copy-Item work.txt workfile.txt
#(Get-Content Header.txt, work.txt, Trailer.txt -readcount 1000)|Set-Content "H:\Documentation\Projects\CVS\StageCVS\CVSMemberEligibility"
Add-Content workfile.txt -value (get-content Trailer.txt)
.\workfile.txt = $header + (gc workfile.txt) 

Solution

  • So as it turns out out-file and get-content are not very performance enhanced. I found that it was taking over 5 minutes to run 5000 record result set and write/read the data.

    When i researched some different performance options for Powershell I found the streamwriter .NET method. For the same process this ran in under 15 seconds.

    Being that my result set in production environment would be 70-90000 records this was the approach I took.

    Here is what i did:

    [IO.Directory]::SetCurrentDirectory("H:\Documentation\Projects\CVS\StageCVS")
    Set-Location "H:\Documentation\Projects\CVS\StageCVS"
    Copy-Item ".\ELIGFINAL.txt" H:\Documentation\Projects\CVS\StageCVS\archive\ELIGFINAL(Get-Date -f yyyyMMdd).txt
    Clear-Content "H:\Documentation\Projects\CVS\StageCVS\ELIGFINAL.txt"
    Copy-Item work.txt workfile.txt
    Add-Content workfile.txt -value (get-content Trailer.txt)
    
    
    $work = ".\workfile.txt"
    $output = "H:\Documentation\Projects\CVS\StageCVS\ELIGFINAL.txt"
    $readerwork = [IO.File]::OpenText("H:\Documentation\Projects\CVS\StageCVS\workfile.txt")
    $readerheader = [IO.File]::OpenText("H:\Documentation\Projects\CVS\StageCVS\Header.txt")
    try
    {
        $wStream = New-Object IO.FileStream $output ,'Append','Write','Read'
    
        $writer = New-Object System.IO.StreamWriter $wStream
        #$write-host "OK"
    }
    finally
    {
    
    }
    $writer.WriteLine($readerheader.ReadToEnd())
    $writer.flush()
    
    $writer.WriteLine($readerwork.ReadToEnd())
    
    $readerheader.close()
    $readerwork.close()
    $writer.flush()
    $writer.close()