Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Reading a text file and retaining line feeds

Reply
 
Old 04-20-2008   #1 (permalink)
ssg31415926


 
 

Reading a text file and retaining line feeds

I've tried reading a text file using

$reportContent = get-content -path $tempFilePath

but it loses the line-feeds so I just get one long string. Can I do
it with get-content or do I need to use a StreamReader or something
similar?

My System SpecsSystem Spec
Old 04-20-2008   #2 (permalink)
Marty List


 
 

Re: Reading a text file and retaining line feeds


The get-content cmdlet does remove line-feeds, but it actually reads each
line into a string object, and stores each line in an array. From the help:
Quote:

>get-help get-content
<snip>
DETAILED DESCRIPTION
It reads the content one line at a time and returns an object for each
line.


#Example:
Quote:

>get-content -path test1.txt|write-host
Line1
Line2
Quote:

>$a = get-content -path test1.txt
>$a.Length
2 # 2 objects in the array, not a string of 2 characters
Quote:

>$a[0]
Line1
Quote:

>$a[1]
Line2
Quote:

>set-content -path test2.txt $a
>get-content -path test2.txt|write-host
Line1
Line2
Quote:

>get-content -path test2.txt|write-host -noNewLine
Line1Line2


Most Windows text files end lines with 2 characters, the carriage-return +
line-feed (\r\n or Chr13 + Chr10). Maybe there's something different about
your original text, maybe it comes from Unix/Linux or Mac, and it only
contains a line-feed or carriage-return but not both?


"ssg31415926" <newsjunkmail@xxxxxx> wrote in message
news:8c837cc7-dfb8-425f-8eaa-102c731922d2@xxxxxx
Quote:

> I've tried reading a text file using
>
> $reportContent = get-content -path $tempFilePath
>
> but it loses the line-feeds so I just get one long string. Can I do
> it with get-content or do I need to use a StreamReader or something
> similar?
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Reading A Text File VB Script
Reading text file at a URL VB Script
reading last line of file VB Script
Reading a text file with StreamReader .NET General
reading a big file line by line to be "out of memory" safe PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46