Skip to content

Instantly share code, notes, and snippets.

@bigga
Last active April 6, 2022 08:52
Show Gist options
  • Save bigga/dd040f5c8a6fb3bbc5bc429abd2b73e6 to your computer and use it in GitHub Desktop.
Save bigga/dd040f5c8a6fb3bbc5bc429abd2b73e6 to your computer and use it in GitHub Desktop.
PowerShell - File Spliting
#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"
@bigga
Copy link
Author

bigga commented Apr 6, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment