# Iterating over files in a directory and including them

I have a folder called diary which contains a series of files:

• 20120101.tex
• 20120105.tex
• 20120304.tex
• etc.

How can I create a single LaTeX document that includes each of these files, in alphabetical order? I'd like to do this within LaTeX, not using LuaTeX or shell operations (to make it portable).

I found a near-solution here, but I can't get it to work properly for this situation.

\documentclass{book}
\usepackage{xifthen}

\begin{document}

% 20120101.tex included here
% 20120105.tex included here
% etc

\end{document}


The code from the near solution linked above for iterating over files that I am trying to modify is:

\foreach \Year in {\StartYear,...,\EndYear}
{   \foreach \Month in {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}
{   \foreach \Day in {1,...,31}
{   \IfFileExists{\Year/\Month/\Day}
\openin\mysource=\Year/\Month/\Day.tex
\closein\mysource
\xdef\writetitle{1}
\begin{loggentry}{\Year - \Month - \Day}{\firstline}
\xdef\writetitle{0}
\input{\Year/\Month/\Day}
\end{loggentry}
}
{   % files does not exist, so nothing to do
}

}
}
}


Here is my failed modification that I want to not look in year/month subdirectories, but read the files as I have listed them above:

\foreach \Year in {\StartYear,...,\EndYear}
{   \foreach \Month in {01,02,03,04,05,06,07,08,09,10,11,12}
{   \foreach \Day in {01,...,31}
{   \IfFileExists{{Year}{Month}{Day}}
\openin\mysource={Year}{Month}{Day}.tex
\closein\mysource
\xdef\writetitle{1}
\begin{loggentry}{\Year - \Month - \Day}{\firstline}
\xdef\writetitle{0}
\input{{YearMonth/\Day}
\end{loggentry}
}
{   % files does not exist, so nothing to do
}

}
}
}


The problem is in concatenating the variables to create the file name instead of subdirectories, which I can't figure out how to do (the above, obviously, doesn't work).

-
–  Peter Grill Oct 23 '12 at 2:52
–  Peter Grill Oct 23 '12 at 2:53
If you are still unable to get this to work, you should post a a fully compilable MWE including \documentclass and the appropriate packages that you used and we can help you from there. –  Peter Grill Oct 23 '12 at 2:55
Just realized that those questions assume that you individual files are set to be able to be imported into a document. For that I'd recommend using the standalone package. There are numerous example of that on this site. –  Peter Grill Oct 23 '12 at 3:17
\foreach \Y in {2012,...2013}{\foreach \M in {1,...,12}{\foreach \D in {1,...,31}{\IfFileExists{\Y\M\D} {\input{\Y\M\D}}{}}}} should work. –  Peter Grill Oct 23 '12 at 5:19

Here is an adaption of my earlier answer from How to iterate through the name of files in a folder:

## Notes:

• This assumes that all the files are named as YYYYMMDD.tex.
• All the files are ready to be \input.
• You can adjust the values of \MinYear \MaxYear to suit.
• There is some overhead here that a check is made for every single day. So unless you are producing a diary over many years this should not really be a problem.
• If you want to be able to typeset each individually diary entry by itself you should have a look at the standalone package. If your dairy entries are quite small this may not be of much value, but if they are lengthy this would be useful as you can view each day as you go and ensure that everything typesets properly and then simply run the main program at certain intervals.
• One reason why your attempt failed is that the code \foreach \Day in {01,...,31} yields the sequence:

   01 2 3 ... 9 10 11 ... 30 31


So while the first day is correct the subsequent days 2-9 are not two digits.

## Code:

\documentclass{article}
\usepackage{pgffor}%

%\usepackage{filecontents}
\begin{filecontents*}{20120201.tex}
Feb 1, 2012:
First day of Feb. Got cold today.
\end{filecontents*}
\begin{filecontents*}{20120202.tex}
Feb 2, 2012:
Second day of Feb. Got even colder today.
\end{filecontents*}

\newcommand*{\MaxYear}{2013}

% http://tex.stackexchange.com/questions/56207/how-to-convert-a-one-digit-number-to-a-two-digit-number
\newcommand\TwoDigits[1]{%
\ifnum#1<10 0#1\else #1\fi
}

\begin{document}
\foreach \YYYY in {\MinYear,...,\MaxYear}{%
\foreach \MM in {1,2,...,12}{%
\foreach \DD in {1,2,...,31}{%
\edef\FileName{\YYYY\TwoDigits{\MM}\TwoDigits{\DD}}
\IfFileExists{\FileName} {%
\par
\input{\FileName}%
}{%
% files does not exist, so nothing to do
}%
}%
}%
}%
\end{document}

-
I am not sure if I agree with the statement about the overhead. I think this will take long if \MaxYear - \MinYear is large. On my desktop the compilation time exceeds 10 seconds when the difference is 10 years. –  Marc van Dongen Oct 23 '12 at 9:19
@MarcvanDongen: Have updated comment regarding runtime. –  Peter Grill Oct 23 '12 at 16:59
@Perter Grill Thanks. –  Marc van Dongen Oct 24 '12 at 6:32