I now fixed more or less the problem replacing this piece of code
by this one
I now get the right number of files.
Replacing this piece of code
with this one
I get the desired number of lines in each file.
BUT after dividing the number of lines evenly on the generated number of files, the last lines in the last file is filled with
"null" statements. Has anybody an idea why?