[Tex/LaTex] How to iterate through the name of files in a folder

external filesfile-lookupfilesystem-access

Assume I have many TeX files. For the sake of simplicity, also asssume they are in a single folder or directory.

I want to input all files from within my main LaTeX document. Actually I can create a list of those files using C# in advance.

To enrich my view about LaTeX or TeX, could you tell me whether or not this job can be done only by using pure TeX or LaTeX?

Best Answer

\documentclass{article}
\makeatletter
\def\app@exe{\immediate\write18}
\def\inputAllFiles#1{%
  \app@exe{ls #1/*.txt | xargs cat >> #1/\jobname.tmp}%
  \InputIfFileExists{#1/\jobname.tmp}{}{}
  \AtEndDocument{\app@exe{rm -f #1/\jobname.tmp}}}
\makeatother
\begin{document}

\inputAllFiles{.}% from the current dir 

\end{document}

Not really difficult. It is only an appetizer of how it can be done. This one only reads files *.txt. You have to run it with pdflatex -shell-escape test

Related Question