A good solution that also works for listings was provided here: How to iterate through the name of files in a folder.
You just have to modify the code like this:
\documentclass{article}
\usepackage{listings}
\makeatletter
\def\app@exe{\immediate\write18}
\def\listDir#1{%
\app@exe{ls #1/* | xargs cat >> \jobname.tmp}%
\lstinputlisting{\jobname.tmp}
\AtEndDocument{\app@exe{rm -f #1/\jobname.tmp}}}
\makeatother
\begin{document}
\listDir{your_directory}
\end{document}
Note that, if you are running under windows, you will have to replace \app@exe
commands by something like (not tested, but based on the answer given in the previous thread):
\app@exe{cmd /c dir /b * > \jobname.tmp}%
\AtEndOfDocument{\app@exe{rm -f #1/\jobname.tmp}}}
The code I gave will include all the files of the directory.
If you want to only include files with a given extension you can specify it directly in \app@exe
command.
Don't forget to compile using pdflatex -shell-escape yourfile.tex
.
LuaLaTeX
you could insert a piece ofLua
script directly in your tex file to scan a folder and write those files to your tex file. Otherwise you could also use a shell script to construct a separate tex file with all the listings, and input that in your main tex file. In both cases some tweaking is necessary depending on the operating system you use (windows, linux, mac).