View Single Post
Old 04-18-2008   #1 (permalink)
Hayato Iriumi


 
 

Read a huge text file from bottom up

I just wanted to share some code with the community today. I had a
need to take a look at a few thousand bytes of a huge text file from
the bottom and I was able to do it in PowerShell.

[Problem]
I manage build servers at my company and somehow the build process
generated a huge text file. The size was well over 600MB and opening
it in Notepad would not have been an option. I didn't even try. So I
had to come up with a way to peek at the last part of the huge text
file.

[Solution]
I thought of Get-Content Cmdlet, but I couldn't find any option that
allowed me to read text file from the bottom. So I turned to .NET
Framework. Here is the code. Please change $TextFilePath and
$BytesToRead as you see fit and please feel free to suggest anything
if there is better to accomplish this.

$TextFilePath = "D:\Temp\HugeTextFile"
$BytesToRead = 2048

$fs = [System.IO.File]::OpenRead($TextFilePath)
$fs.Position = $fs.Length - $BytesToRead

$sr = New-Object System.IO.StreamReader($fs)

$text = $sr.ReadToEnd()

$sr.Close()
$fs.Close()

$text
My System SpecsSystem Spec