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.
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