[Tex/LaTex] How to automatically include several text documents into a LaTeX document

automationincludeinput

I have several .txt or .dat files, whose names are of the form: dn-ddmmyyyy.txt. For instance:

d0-01021989.txt
d1-21021989.txt
d2-15021994.txt
d3-12121996.txt
d4-14032014.txt
d5-22022035.txt
...many more...

What is the best way to include all the files without using manually \include for each one of them?

Is there a way to create a title (like a chapter title) for each document, as "Document N", and extract the number present in the file name, which is a date, to put it in the title?

Is there a way to do this only with TeX?

Best Answer

I don't know about a TeX only solution (I could however imagine that lualatex could by handy) but what I usually do is the following:

1) use a command like dir /b *.tex > allFiles.txt? to get a file of all the TeX files. (the /b suppresses everything but the file name)

2) open up Excel, paste the content of allFiles.txt and use string concatenation to build the include commands.

For once-only this is the most efficient solution. In cases where I regularly have to update my TeX file I'd write a short Python/Bash/Batch file.

EDIT:

I have found some suitable code to include via lualatex Lua-calls in one of the answers here and was able to adjust it a little. It uses \write18 as well, so it must be used with --shell-escape. I am pretty sure that a Lua-only solution is possible, but this is clearly beyond my Lua-knowledge.

%!TEX TS-program = LuaLaTeX

\documentclass[12pt,ngerman]{scrartcl}
\usepackage{luacode}

\begin{document}
Some text before the Lua call.

\begin{luacode}
function scandir(directory)
    local i, t, popen = 0, {}, io.popen
    for filename in popen('dir "'.. directory .. '" /b d*.tex'):lines() do
        i = i + 1
        t[i] = filename
       tex.print('\\input{includes/' .. filename .. '} from ' .. filename ..'\\clearpage')  
    end
    return t
end
scandir("C:/Users/Uwe/LuaLula/includes/")
\end{luacode}

\end{document}
Related Question