advice needed on copying a file and inserting header/footer

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 4 months ago.

  • Author
    Posts
  • #5623

    by tjshaffe at 2013-03-04 14:59:22

    Good Everning, Everyone,
    I am looking for some advice on copying a file (.txt) from one destination to another and inserting a header and footer into the newly created (copied) file.

    The header and footer would include special characters, please see below for an example of what is required

    &LOAD
    ?TITLE1=
    ?DATE1=
    ?COPIES=001

    –text–


    &EJOB

    Certainly any advice would be greatly appreciated.

    -Troy

    by AlexBrassington at 2013-03-04 15:56:32

    How about this?

    #Create here-strings to hold the header and footer text.
    $headerText = @"
    &LOAD
    ?TITLE1=
    ?DATE1=
    ?COPIES=001

    "@

    $footerText = @"

    &EJOB
    "@

    #Get the content from your source file
    $bodyText = Get-Content "D:\Photos\input.txt"

    #Append the header, body and footer
    $OutputText = $headerText + $bodyText + $footerText

    #Output to file
    $OutputText > "D]

    More info on Here strings here: http://technet.microsoft.com/en-us/libr ... 92792.aspx. Note that you need to finish them with the "@ on a new line.

    by tjshaffe at 2013-03-05 08:47:00

    Alex,

    Thank you very much for your follow up. I did reply (since deleted) with additional concerns, however continued testing provided the solution I was looking.

    Again, I appreciate your time. Take care!

    -Troy

    by tjshaffe at 2013-03-05 09:30:28

    Alex,

    The output file is modified in such a way it is losing its formatting. How would you suggest simply building a header / footer?
    I can use... copy-item -path d:\test.txt -destination i:\test.txt ...to get the file where I want without losing the original formatting. If i could then modify that file to insert the header / footer I think I will have the file the way I need.

    Thank you very much!

    by AlexBrassington at 2013-03-05 10:10:04

    I'm afraid you'll have to be more descriptive about what formatting is getting lost and what you need the format to be. The here-strings should keep all the characters and formatting you put in, are you getting issues with the body of the text?

    Can you post up an example of the script you are running and specify where it fails?

    by tjshaffe at 2013-03-05 12:19:39

    Alex,

    Thank you very much for the reply..

    Here is the code I am using..

    #Create here-strings to hold the header and footer text.
    $headerText = @"
    &LOAD AUDR1007
    ?TITLE1=VENDOR INVOICE LIST
    ?DATE1=2011
    ?COPIES=001

    "@

    $footerText = @"


    &EJOB
    "@

    #Get the content from your source file
    $bodyText = Get-Content "D:\test.txt"

    #Append the header, body and footer
    $OutputText = $headerText + $bodyText + $footerText

    #Output to file
    $OutputText > "i:\test.txt"

    Original Text

    93/99/9993 99:39 |XYZ CORP COUNTY |PG 9
    username |VENDOR INVOICE LIST |VENDOR

    DOCUMENT P.O. INV DATE VOUCHER WARRANT CHECK # INVOICE NET DUE DATE TYPE STS INVOICE DESCRIPTION
    ————————————————————————————————————————————-
    99999999 99999999 99/99/9999 9999X 99999999 99999999 999.99 99/99/9999 INV PD INV# 999939999
    INVOICE: 999939999 CHECK DATE: 99/99/9999

    999999 TEST CORP
    99999999 99999999 99/99/9999 9999X 99999999 99999999 999.99 99/99/9999 INV PD INV# 999999999
    INVOICE: 999999999 CHECK DATE: 99/99/9999

    999999 TEST CORP
    99999999 99999999 99/99/9999 9999X 99999999 99999999 999.99 99/99/9999 INV PD INV# 999999999
    INVOICE: 999999999 CHECK DATE: 99/99/9999

    999999 TEST CORP
    99999999 99999999 99/99/9999 9999X 99999999 99999999 999.99 99/99/9999 INV PD INV# 999999999
    INVOICE: 999999999 CHECK DATE: 99/99/9999

    Text after code is ran.

    &LOAD 1007
    ?TITLE1=TEST Title
    ?DATE1=12/31/2012
    ?COPIES=001

    93/99/9993 99:39 |XYZ CORP COUNTY |PG 9 username |VENDOR INVOICE LIST |VENDOR DOCUMENT P.O. INV DATE VOUCHER WARRANT CHECK # INVOICE NET DUE DATE TYPE STS INVOICE DESCRIPTION ————————————————————————————————————————————- 99999999 99999999 99/99/9999 9999X 99999999 99999999 999.99 99/99/9999 INV PD INV# 999939999 INVOICE: 999939999 CHECK DATE: 99/99/9999 999999 TEST CORP 99999999 99999999 99/99/9999 9999X 99999999 99999999 999.99 99/99/9999 INV PD INV# 999999999 INVOICE: 999999999 CHECK DATE: 99/99/9999 999999 TEST CORP 99999999 99999999 99/99/9999 9999X 99999999 99999999 999.99 99/99/9999 INV PD INV# 999999999 INVOICE: 999999999 CHECK DATE: 99/99/9999 999999 TEST CORP 99999999 99999999 99/99/9999 9999X 99999999 99999999 999.99 99/99/9999 INV PD INV# 999999999 INVOICE: 999999999 CHECK DATE: 99/99/9999


    &EJOB

    This doesnt seem to be a word wrap issue, because i have tried it viewing the data both ways.

    by Malkey at 2013-03-06 22:18:20

    It appears the joining of an array to a here-string using a "+", omits the carriage return/line feed characters from between the elements of the array. The array that was formed by using "Get-Content".

    These two method work on my win7 system.
    #Create here-strings to hold the header and footer text.
    $headerText = @"
    &LOAD 1007
    ?TITLE1=TEST Title
    ?DATE1=12/31/2012
    ?COPIES=001

    "@
    $footerText =
    @"


    &EJOB
    "@

    #Output to file
    $headerText > ".\Output.txt"

    #Append body
    Get-Content .\input.txt >> .\Output.txt

    #Append footer
    $footerText >> .\Output.txt

    or
    #Create here-strings to hold the header and footer text.
    $headerText = @"
    &LOAD 1007
    ?TITLE1=TEST Title
    ?DATE1=12/31/2012
    ?COPIES=001

    "@
    $footerText =
    @"


    &EJOB
    "@

    #Get the content from your body file
    $bodyText = Get-Content ".\input.txt"

    #Join the header, body and footer
    $OutputText = $headerText + ($bodyText | Out-String) + $footerText

    #Output to file
    $OutputText > ".\Output.txt"

    Thanks to AlexBrassington for his example on which my examples are based.

    by tjshaffe at 2013-03-07 07:05:16

    Malkey, AlexBrassington,

    Thank you both very much for your time. The collaboration among both of you have helped tremendously. I am now able to achieve what is needed. Cheers, guys!

    -Troy

You must be logged in to reply to this topic.