Make sure to change the 2 file path variables at the start of the code to your location of the input/output files.
I know the code is messy and not well commented but this is it so far.
It's a start and we can test the crap out of it, modify it, or scrap it.