PowerShell, how to append blank lines to an existing file

Answered PowerShell, how to append blank lines to an existing file

  • Monday, February 11, 2013 3:55 PM
     
     

    Hi, 

    I tried to append blank lines to an existing file, but all I got are square-box looked chars at the end of the file. 

    I must have done it wrong. What's the correct way doing that?

    Thanks

All Replies

  • Monday, February 11, 2013 4:02 PM
    Moderator
     
     

    What did you try?

    Bill

  • Monday, February 11, 2013 4:43 PM
     
      Has Code
    "`n`n" | Out-File -FilePath $File -Encoding "ASCII" -Append

  • Monday, February 11, 2013 4:49 PM
    Moderator
     
     Answered Has Code

    Well, a quoted string is going to have a implicit line terminator, so

    "" | out-file $file -append -encoding ASCII

    is going to output a single empty line to the end of $file. If you want two empty lines, you probably want this:

    "`r`n" | out-file $file -append -encoding ASCII

    Also not that I used `r`n (carriage return followed by line feed), not `n`n (two consecutive line feeds).

    Bill

    • Marked As Answer by smetah Monday, February 11, 2013 6:49 PM
    •  
  • Monday, February 11, 2013 5:57 PM
     
      Has Code
    thanks. Did you give it a try (of
    "`r`n" | out-file $file -append -encoding ASCII
    )?
  • Monday, February 11, 2013 6:05 PM
    Moderator
     
     
    thanks. Did you give it a try (of
    "`r`n" | out-file $file -append -encoding ASCII
    )?

    Yes, I tested it before I posted it and it worked for me.

    Bill

  • Monday, February 11, 2013 6:48 PM
     
     

    Somehow it never worked for me. I got square-box looked chars at the end of the file no matter what, until I deleted the existing file. and all of sudden everything works now. 

    Thanks. 

  • Monday, February 11, 2013 6:53 PM
    Moderator
     
     

    That can happen if you have a mismatched group of new-line characters in a file, depending on what editor program you're using to view/open the file.

    Bill

  • Monday, February 11, 2013 10:59 PM
     
     

    I guess so. 

    Thanks.