I know you found an exact answer, but I thought I’d give an alternative. You could do this or something similar with pandoc [1] and a make file. I tend to write my files in markdown run a makefile that concatenates them and converts them with pandoc to pdf, html, or docx.
[1]: http://pandoc.org