Last active
November 8, 2021 18:37
-
-
Save randyburden/35ca1354f727f5d483fbc09e79e94962 to your computer and use it in GitHub Desktop.
Splits a large log file using PowerShell. Tested against very large multi-terabyte log files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Split Large Log File | |
$inputFile = "C:\temp\VeryLargeLogFile.log" | |
$outputDirectory = "C:\temp\logFileSplit" | |
$outputFileName = "LogFile" | |
$outputFileExtension = "log" | |
$upperBound = 500MB | |
New-Item -ItemType Directory -Force -Path $outputDirectory | |
$fromFile = [io.file]::OpenRead($inputFile) | |
$buff = new-object byte[] $upperBound | |
$count = $idx = 0 | |
try { | |
do { | |
"Reading $upperBound" | |
$count = $fromFile.Read($buff, 0, $buff.Length) | |
if ($count -gt 0) { | |
$to = "{0}\{1}.{2}.{3}" -f ($outputDirectory, $outputFileName, $idx, $ext) | |
$toFile = [io.file]::OpenWrite($to) | |
try { | |
"Writing $count to $to" | |
$tofile.Write($buff, 0, $count) | |
} finally { | |
$tofile.Close() | |
} | |
} | |
$idx ++ | |
} while ($count -gt 0) | |
} | |
finally { | |
$fromFile.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment