none
How to read a file as a string instead of an array? RRS feed

  • Question

  • My purpose is to know which char is used as row terminator, whether \n or \r\n. So I need to first get the file as a string and the find whether there are \n or \r\n inside the string. When use get-content, it already splits the file into lines as array.

    Wednesday, January 19, 2011 3:12 AM

Answers

  • I would say that it won't help with this task:

    New-Item -ItemType file -Path Test.txt -Value "Test`nTest`nTest"
    (gc .\Test.txt | Out-String) -match '\r\n'
    True
    [system.io.file]::ReadAllText((Resolve-Path .\Test.txt).ProviderPath) -match '\r\n'
    False
    [system.io.file]::ReadAllText((Resolve-Path .\Test.txt).ProviderPath) -match '\n'
    True
    
    I would go with .NET-way for this...
    Wednesday, January 19, 2011 10:53 AM
  • Agreed.  Use [Io.File]::ReadAllText($path) to read the entire file into a single string.

    Using any standard powershell cmdlet will automatically split the file into separate lines, at which point you've already lost the needed information.

     

    Thanks,
    -Lincoln

    • Marked as answer by Daniel Wu Thursday, January 20, 2011 8:38 AM
    Wednesday, January 19, 2011 3:43 PM

All replies

  • Try use Select-String.

     

    How Can I Search a Text File for Strings Meeting a Specified Pattern?

     

    Alternatively, you can add the quotation marks to convert the output of get-content from array to string.

     

    Best Regards

    Dale


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    Wednesday, January 19, 2011 9:59 AM
    Moderator
  • Pipe the content to the Out-String cmdlet:

     

    Get-Content test.txt | Out-String


    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    • Proposed as answer by Márk Bécsy Monday, December 17, 2012 11:23 AM
    Wednesday, January 19, 2011 10:34 AM
    Moderator
  • I would say that it won't help with this task:

    New-Item -ItemType file -Path Test.txt -Value "Test`nTest`nTest"
    (gc .\Test.txt | Out-String) -match '\r\n'
    True
    [system.io.file]::ReadAllText((Resolve-Path .\Test.txt).ProviderPath) -match '\r\n'
    False
    [system.io.file]::ReadAllText((Resolve-Path .\Test.txt).ProviderPath) -match '\n'
    True
    
    I would go with .NET-way for this...
    Wednesday, January 19, 2011 10:53 AM
  • Agreed.  Use [Io.File]::ReadAllText($path) to read the entire file into a single string.

    Using any standard powershell cmdlet will automatically split the file into separate lines, at which point you've already lost the needed information.

     

    Thanks,
    -Lincoln

    • Marked as answer by Daniel Wu Thursday, January 20, 2011 8:38 AM
    Wednesday, January 19, 2011 3:43 PM
  • Since PowerShell 3.0 you can use the Raw Parameter of Get-Content to get the text as a single string instead of an array of strings / lines. See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content
    • Proposed as answer by jrvModerator Thursday, November 29, 2018 6:43 PM
    Thursday, November 29, 2018 9:03 AM
  • Agree.  "Get-Content -Raw"

    Thursday, November 29, 2018 2:02 PM