File splitting

I’ve recently had to do a lot of file splitting e.g. taking lists of names and creating lists of alternate names to give me two files. Every time I have one of these problems I seem to re-write the script.  Time for a generic file splitting function.

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021

function split-file {
[CmdletBinding()]
param
 (
 
[string]$file="data.txt",
  [string[]]$outfiles
) 

 
if (!(Test-Path $file)){throw "File $file not found"
}
 

## delete previous files
 $num = $outfiles.
count
 
foreach ($ofile in $outfiles
) {
 
if (Test-Path $ofile){Remove-Item $ofile
}
 }
 
 
$data = Get-Content $file
 
 
for($i=0; $i -le $data.count; $i++
){
  
$fnum = $i % $num
   $data[$i] | Out-File -FilePath $($outfiles[$fnum]) -Append
 }
}

Input parameters are the file to split and the list of files as output – this also give the number of splits.

Test that the input file can be found and count the number of output files.

For each of the output files test if it exists and delete it.

Read the input data.

Loop through the input data – calculate a number based on the modulus from the counter and the number of output files. This gives us which file in the array to output the data.

Finally write out the data – remembering to append.

This is a rather simple splitting scenario but will satisfy many requirements. Next I want to modify this so that the output wraps i.e. the output is not written 1,2,3,1,2,3,1,2,3 etc  but is written 1,2,3,3,2,1,1,2,3,3,2,1 etc

Leave a Reply