All the solutions to this kind of problems that I have seen until now have recourse to shell scripts or temporary files created with \write18
and subsequently \input
(ted). But it may be worth noting that it is also possible to use pipes instead of temporary files. Actually, I am not sure that the “piped input” feature works with \openin
, as well as with \input
, on all systems, but on mine (a MacTeX/TeXLive installation) it does.
To construct an (almost) minimal working example, create a file Main.tex
placed alongside of a subdirectory Data
, into which, in turn, you put, say, four files named Foo.tex
, Bar.tex
, Baz.tex
, and AnotherFileName.tex
. These four files could read as follows (for example):
Contents of Foo.tex
:
\subsection{The \emph{Foo} file}
\lipsum[2]
Contents of Bar.tex
:
\subsection{The \emph{Bar} file}
\lipsum[3]
Contents of Baz.tex
:
\subsection{The \emph{Baz} file}
\lipsum[4]
Contents of AnotherFileName.tex
:
\subsection{There is of course a problem\ldots}
\ldots~the order of file inclusion depends on the order in which they are
listed by the \texttt{ls} command; but I~am afraid this cannot be avoided!
\lipsum[5-8]
You are now ready to test the (A)MWE, which consists of the file Main.tex
with the following contents:
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\makeatletter
\@ifdefinable\@fileliststream{\newread\@fileliststream}
\@ifdefinable\@currentfilename{}
\newcommand*\InputAllFilesInSubfolder[1]{
% #1 <- Path to subfolder: MUST end with the (operating system dependent)
% character for separating path components.
\openin\@fileliststream |"ls '#1'"\relax
\loop \unless\ifeof\@fileliststream
\begingroup
\endlinechar \m@ne % cf. exercise 20.18
\global\readline\@fileliststream to\@currentfilename
\endgroup
\ifx\@currentfilename\@empty \else
\typeout{Inputting "#1\@currentfilename".}%
\input{#1\@currentfilename}%
\fi
\repeat
\closein\@fileliststream
}
\makeatother
\begin{document}
\tableofcontents
\section{Main section}
\lipsum[1]
\InputAllFilesInSubfolder{Data/}
\end{document}
In this case, I think that showing the output is not particularly meaningful.
Addition
Pretty obviously, however, the options of the ls
command give you some control over the order in which the files are input(ted): you might want to try, e.g.,
\openin\@fileliststream |"ls -rtU '#1'"\relax