![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Why does get-content on a text file remove crlf when cast to a string? Given the following: function doWork($fileContent) { write-output $fileContent } gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } will result in the contents of all AssemblyInfo.cs files being dumped to the screen. However, this function doWork([string]$fileContent) { write-output $fileContent } gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } results in all crlf'ds being removed. I am trying to update version information and I have a reg-ex in 'doWork' that does the updates but the problem is that whenver the results of get-contents is processed as a string, all the crlf information vanishes which results in corrupted files? What am I doing wrong? thanks -mark -- -mark |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Why does get-content on a text file remove crlf when cast to a string? Get-Content reads a file as a set of lines. You can use alternative characters as the record delimiter if you want; for example, you can force it to assume that the delimiter should be the null character by using this: gc AssemblyInfo.cs -Delimiter `0 This ensures you get back a complete raw string. "Mark" <mwatts at hotmail dot com> wrote in message news:4DBDB20F-A444-47F4-AA33-419B4C2B3C99@microsoft.com... > > Given the following: > > function doWork($fileContent) > { > write-output $fileContent > } > > gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } > > will result in the contents of all AssemblyInfo.cs files being dumped to > the screen. However, this > > function doWork([string]$fileContent) > { > write-output $fileContent > } > > gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } > > results in all crlf'ds being removed. > > I am trying to update version information and I have a reg-ex in 'doWork' > that does the updates but the problem is that whenver the results of > get-contents is processed as a string, all the crlf information vanishes > which results in corrupted files? > > What am I doing wrong? > > thanks > -mark > > -- > -mark |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Why does get-content on a text file remove crlf when cast to a string? Get-Content returns the contents of the file as a collection of strings (one string per line). Since you are casting the collection to a string, the CRLFs are lost. Instead, join can be used as shown below to obtain a single string. gci -i Assemblyinfo.cs -r | %{$contents=gc $_.FullName;doWork([system.string]::join("`r`n", $contents)) } The above will display the contents on the screen in the same way like the first snippet. -- Narayanan Lakshmanan [MSFT] Windows PowerShell Development Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "Mark" <mwatts at hotmail dot com> wrote in message news:4DBDB20F-A444-47F4-AA33-419B4C2B3C99@microsoft.com... > > Given the following: > > function doWork($fileContent) > { > write-output $fileContent > } > > gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } > > will result in the contents of all AssemblyInfo.cs files being dumped to > the screen. However, this > > function doWork([string]$fileContent) > { > write-output $fileContent > } > > gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } > > results in all crlf'ds being removed. > > I am trying to update version information and I have a reg-ex in 'doWork' > that does the updates but the problem is that whenver the results of > get-contents is processed as a string, all the crlf information vanishes > which results in corrupted files? > > What am I doing wrong? > > thanks > -mark > > -- > -mark |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Why does get-content on a text file remove crlf when cast to a string? "Narayanan Lakshmanan [MSFT]" <narayanl@online.microsoft.com> wrote in message news:eJ%23w5JC3GHA.4924@TK2MSFTNGP05.phx.gbl... Thank you, that answers my questions ![]() > Get-Content returns the contents of the file as a collection of strings > (one string per line). Since you are casting the collection to a string, > the CRLFs are lost. Instead, join can be used as shown below to obtain a > single string. > > gci -i Assemblyinfo.cs -r | %{$contents=gc > $_.FullName;doWork([system.string]::join("`r`n", $contents)) } > > The above will display the contents on the screen in the same way like the > first snippet. > > -- > Narayanan Lakshmanan [MSFT] > Windows PowerShell Development > Microsoft Corporation > This posting is provided "AS IS" with no warranties, and confers no > rights. > > > "Mark" <mwatts at hotmail dot com> wrote in message > news:4DBDB20F-A444-47F4-AA33-419B4C2B3C99@microsoft.com... >> >> Given the following: >> >> function doWork($fileContent) >> { >> write-output $fileContent >> } >> >> gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } >> >> will result in the contents of all AssemblyInfo.cs files being dumped to >> the screen. However, this >> >> function doWork([string]$fileContent) >> { >> write-output $fileContent >> } >> >> gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } >> >> results in all crlf'ds being removed. >> >> I am trying to update version information and I have a reg-ex in 'doWork' >> that does the updates but the problem is that whenver the results of >> get-contents is processed as a string, all the crlf information vanishes >> which results in corrupted files? >> >> What am I doing wrong? >> >> thanks >> -mark >> >> -- >> -mark > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Why does get-content on a text file remove crlf when cast to a string? "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message news:eQPKdDA3GHA.2228@TK2MSFTNGP03.phx.gbl... Thanks Alex. > Get-Content reads a file as a set of lines. > > You can use alternative characters as the record delimiter if you want; > for example, you can force it to assume that the delimiter should be the > null character by using this: > > gc AssemblyInfo.cs -Delimiter `0 > > This ensures you get back a complete raw string. > > > > "Mark" <mwatts at hotmail dot com> wrote in message > news:4DBDB20F-A444-47F4-AA33-419B4C2B3C99@microsoft.com... >> >> Given the following: >> >> function doWork($fileContent) >> { >> write-output $fileContent >> } >> >> gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } >> >> will result in the contents of all AssemblyInfo.cs files being dumped to >> the screen. However, this >> >> function doWork([string]$fileContent) >> { >> write-output $fileContent >> } >> >> gci . -i AssemblyInfo.cs -r | % { doWork (gc $_.FullName) } >> >> results in all crlf'ds being removed. >> >> I am trying to update version information and I have a reg-ex in 'doWork' >> that does the updates but the problem is that whenver the results of >> get-contents is processed as a string, all the crlf information vanishes >> which results in corrupted files? >> >> What am I doing wrong? >> >> thanks >> -mark >> >> -- >> -mark > > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Why does get-content on a text file remove crlf when cast to a string? >> >> gci -i Assemblyinfo.cs -r | %{$contents=gc >> $_.FullName;doWork([system.string]::join("`r`n", $contents)) } >> or you can use .Net [System.IO.File]::ReadAllText(Assemblyinfo.cs) |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Why does get-content on a text file remove crlf when cast to a string? "XShell" <xshell@nospam.com> wrote in message news:ehbHZ3D3GHA.4756@TK2MSFTNGP04.phx.gbl... >>> >>> gci -i Assemblyinfo.cs -r | %{$contents=gc >>> $_.FullName;doWork([system.string]::join("`r`n", $contents)) } >>> > > or you can use .Net > > [System.IO.File]::ReadAllText(Assemblyinfo.cs) Actually that was what I did while waiting for the answer to my newsgroup question. But while that it the ".NET way" it is not the "PowerShellWay". ![]() |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Why does get-content on a text file remove crlf when cast to a string? There is often a distinction, but usually there is not. We designed PowerShell to allow you to smoothly glide between typical scripting tasks and more complex programming tasks -- our support for .Net being a key to this spectacular amount of breadth. For example, PowerShell will likely not ever support random I/O on a file stream. That's a task that the .Net framework handles well, using a model that customers are familiar with. It doesn't make much sense for PowerShell to re-implement all of that, just as there will always be sections of the Win32 API that .Net users need to call through P/Invoke techniques. As you move away from goals obviously covered by language primitives and stock cmdlets, there comes a point where it just makes sense to get on the powerful bridge of .Net interop. In this specific case, the ReadAllText() method on [System.IO.File] really is the best solution for PowerShell V1. (This is actually an option that we hope to add to Get-Content in the future, though) -- Lee Holmes [MSFT] Windows PowerShell Development Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "Mark" <mwatts at hotmail dot com> wrote in message news:E6BEC1EF-F8CA-45A0-A9B6-4110D28FA7E5@microsoft.com... > "XShell" <xshell@nospam.com> wrote in message > news:ehbHZ3D3GHA.4756@TK2MSFTNGP04.phx.gbl... >>>> >>>> gci -i Assemblyinfo.cs -r | %{$contents=gc >>>> $_.FullName;doWork([system.string]::join("`r`n", $contents)) } >>>> >> >> or you can use .Net >> >> [System.IO.File]::ReadAllText(Assemblyinfo.cs) > > Actually that was what I did while waiting for the answer to my newsgroup > question. But while that it the ".NET way" it is not the "PowerShell> Way". ![]() > > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| getting text file content from remote computer | PowerShell | |||
| Output all content of text file in different columns | PowerShell | |||
| Using Add-Content to add text to a certain location of a file. | PowerShell | |||
| HowTo: Get Text from a file and put in a string. | PowerShell | |||