locked
how to include streamwriter RRS feed

  • Question

  •   I've been searching on how to include streamwriter since this file I'm do a replace in is large and want to as fast as
    possible create the new output file.

    $Start = Get-Date
    #
    # Start
    #
    
    $LogFilePath = "C:\compress_in\usermstr.csv"
    $FileStream = New-Object -TypeName IO.FileStream -ArgumentList ($LogFilePath), ([System.IO.FileMode]::Open), ([System.IO.FileAccess]::Read), ([System.IO.FileShare]::ReadWrite);
    $ReadLogFile = New-Object -TypeName System.IO.StreamReader -ArgumentList ($FileStream, [System.Text.Encoding]::ASCII, $true);
     
    [int]$LineNumber = 0;
    
     
    # Read Lines
    while (!$ReadLogFile.EndOfStream)
    {
    	$LogContent = $ReadLogFile.ReadLine()
    	$LineNumber++    
     
        $data = $LogContent.Replace('"','')
    
            
    	}
    $LineNumber
    $ReadLogFile.Close()
    $End = Get-Date
    # Show the result
    New-Timespan -Start $Start -End $End
     I want to create a new output file with the quote marks removed.

     Thanks.

    Thursday, August 9, 2018 7:25 PM

Answers

  • If you don't know how to sue stream IO then don't use it.  The PS CmdLets are nearly as efficient because they use the stream IO methods.

    Since you clearly have no programming or scripting experience I recommend first learning PowerShell.  This forum is not equipped to give personal lessons in how to write a script.

    1. Learn PowerShell  
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    \_(ツ)_/

    • Marked as answer by hart60 Friday, August 10, 2018 12:11 AM
    Thursday, August 9, 2018 11:48 PM

All replies

  • $f = Get-Item yourfile.ext
    $strm = $f>Open('OpenOrCreate')
    $strm.Write($bytes,0,$bytes.Length)
    $strm.Flush($true)
    $strm.Close()

    Files come with a building stream reader and writer.

    For a new file do this:

    $f = [System.IO.FileInfo]'d:\test\anewfile.txt'
    $strm = $f.Open('OpenOrCreate')

    You now have a brand new file with a StreamWriter.

    To read just use the "Read" options on the file object.

    "ReadiLine" is not noticeably faster than using Get-Content.

    Get-Content file -ReadCount 100 | %{ <# process reads #> }

    help get-Content -online


    \_(ツ)_/



    • Edited by jrv Thursday, August 9, 2018 8:01 PM
    Thursday, August 9, 2018 7:58 PM
  •  Im trying to get the $data into the STreamWriter

    $data = $LogContent.Replace('"','')

     To create the NEW file without the Commas.

     Thanks.

    Thursday, August 9, 2018 11:08 PM
  • If you don't know how to sue stream IO then don't use it.  The PS CmdLets are nearly as efficient because they use the stream IO methods.

    Since you clearly have no programming or scripting experience I recommend first learning PowerShell.  This forum is not equipped to give personal lessons in how to write a script.

    1. Learn PowerShell  
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    \_(ツ)_/

    • Marked as answer by hart60 Friday, August 10, 2018 12:11 AM
    Thursday, August 9, 2018 11:48 PM
  •  Ok Thanks.

      will work thru your suggestions.

    Friday, August 10, 2018 12:11 AM