Remove last line from file with Powershell

后端 未结 5 990

I am using

gc FileWithEmptyLines.txt | where {$_ -ne \"\"} > FileWithNoEmptyLines.txt

to remove the empty lines that SSRS puts at the b

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 04:27

    For 'UCS-2 Little Endian' file format, use this:

    $stream = [IO.File]::Open($filename, [IO.FileMode]::Open)
    $stream.Position = $stream.Length - 4
    $bytes = 0..3 | %{ $stream.ReadByte() }
    $compareBytes = 13,0,10,0 # CR,LF
    echo "bytes: "$bytes
    if ("$bytes" -eq "$compareBytes")
    {
        $stream.SetLength($stream.Length - 4)
    }
    $stream.Close()
    $stream.Dispose()
    

提交回复
热议问题