Last active
April 6, 2022 08:52
-
-
Save bigga/dd040f5c8a6fb3bbc5bc429abd2b73e6 to your computer and use it in GitHub Desktop.
PowerShell - File Spliting
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 test | |
$sw = new-object System.Diagnostics.Stopwatch | |
$sw.Start() | |
$filename = "IN_FILENAME.ext" | |
$rootName = "OUT_FILENAME_" | |
$ext = "ext" | |
$linesperFile = 2000#2k | |
$filecount = 1 | |
$reader = $null | |
try{ | |
$reader = [io.file]::OpenText($filename) | |
try{ | |
"Creating file number $filecount" | |
$writer = [io.file]::CreateText("{0}{1}.{2}" -f ($rootName,$filecount.ToString("000"),$ext)) | |
$filecount++ | |
$linecount = 0 | |
while($reader.EndOfStream -ne $true) { | |
"Reading $linesperFile" | |
while( ($linecount -lt $linesperFile) -and ($reader.EndOfStream -ne $true)){ | |
$writer.WriteLine($reader.ReadLine()); | |
$linecount++ | |
} | |
if($reader.EndOfStream -ne $true) { | |
"Closing file" | |
$writer.Dispose(); | |
"Creating file number $filecount" | |
$writer = [io.file]::CreateText("{0}{1}.{2}" -f ($rootName,$filecount.ToString("000"),$ext)) | |
$filecount++ | |
$linecount = 0 | |
} | |
} | |
} finally { | |
$writer.Dispose(); | |
} | |
} finally { | |
$reader.Dispose(); | |
} | |
$sw.Stop() | |
Write-Host "Split complete in " $sw.Elapsed.TotalSeconds "seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Splitting files into smaller files with X lines (Here, 2000 lines / file) using PowerShell in Windows.
Taken from:
https://stackoverflow.com/questions/1001776/how-can-i-split-a-text-file-using-powershell