# Concatenating files created with filecontents

I am creating a large number of data files "on the fly" using the filecontents package.

I need to be able to concatenate these files in various permutations For example in the MWE below, I would need to be able to create a new file consisting of data2+data1 and so on for various purposes. Is there any way to do this? I suppose the question might be similar to asking whether I can expand a macro/file input within a verbatim?

\documentclass[12pt]{article}
\usepackage{filecontents}
\begin{document}

\begin{filecontents*}{data1.tex}
Line 1
Line 2
\end{filecontents*}

\begin{filecontents*}{data2.tex}
Line 3
Line 4
\end{filecontents*}

\begin{filecontents*}{data3.tex}
Line 5
Line 6
\end{filecontents*}

\end{document}

• you could read them into tex and write them out but simpler if you allow --shell-escape would be \immediate\write18{cat data2.tex data1.tex > foo.tex} – David Carlisle Apr 13 '15 at 16:52
• That looks promising. However when I add line you suggest to mwe I do get a foo.tex but that is empty (even after a second pass). I am calling latex as ..\win32\latexmk.exe" -f -xelatex --shell-escape mwe.tex, and all other shell functions work fine for me. (How would I do your first method?) – Aubrey Blumsohn Apr 13 '15 at 20:05
• What does work is \immediate\write18{% echo "" > foobar.txt; echo "" >> foobar.txt; echo "--------" >> foobar.txt; echo "Foo: foo" >> foobar.txt; echo "Foo: foo" >> foobar.txt; echo "Bar: bar" >> foobar.txt } -- but that is very complicated versus your idea – Aubrey Blumsohn Apr 13 '15 at 20:28
• Probably because in windows there is no cat.... so what is non shell method as I don't much like mine – Aubrey Blumsohn Apr 13 '15 at 20:47
• Actually I'm on windows but use the cygwin bash shell so have cat and things,. this answer has some windows alternatives that shoudl work stackoverflow.com/questions/2477271/… – David Carlisle Apr 13 '15 at 20:59

You could read them into tex and write them out but simpler if you allow --shell-escape would be
\immediate\write18{cat data2.tex data1.tex > foo.tex}

(cat is available for windows, as noted in comments).