It`s simple when you know how ! Tutorials,Windows How to Split Very Large Log File To Smaller size files

How to Split Very Large Log File To Smaller size files

Log files can get very large in size making them very hard to work with. There are many ways to split log files, with applications or split command, but I found this Powershell script that did this task and give you some option in the process :

$SourceFile = "C:\path\to\large.log"
$MaxSize = 100MB
$Prefix = "smalllog"
$Counter = 1

$Reader = New-Object System.IO.StreamReader($SourceFile)
$OutputFile = New-Object System.IO.FileStream("$Prefix$Counter.log", 'Create', 'Write', 'ReadWrite')
$Buffer = New-Object byte[] 1024

do {
    $BytesRead = $Reader.BaseStream.Read($Buffer, 0, $Buffer.Length)
    if ($BytesRead -gt 0) {
        $OutputFile.Write($Buffer, 0, $BytesRead)
        if ($OutputFile.Length -gt $MaxSize) {
            $OutputFile.Close()
            $Counter++
            $OutputFile = New-Object System.IO.FileStream("$Prefix$Counter.log", 'Create', 'Write', 'ReadWrite')
        }
    }
} while ($BytesRead -gt 0)

$Reader.Close()
$OutputFile.Close()

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post