Iterate through files in specific directory

While trying to enhance one of my packages, I wondered whether it is possible to iterate through a directory's file list in expl3 (I've read in l3interfaces.pdf that expl3 has some IO facilities, but they seem unable to scan directories).

The use case is the following: I have one main document. This shall include some special files generated with an external application and stored in directories outside the path.

So the question is: How would an expl3 "directory scanner" (I only want to specify the directory) look or do I have to use Lua for this purpose?

• I assume that you specify only the directory? – user31729 Apr 21 '17 at 15:06
• @ChristianHupfer Yes. – TeXnician Apr 21 '17 at 15:06
• Perhaps this helps: tex.stackexchange.com/questions/341578/…. TeX/LaTeX isn't really designed to work with directories – user31729 Apr 21 '17 at 15:15
• @ChristianHupfer Does not sound bad, but I'd rather use a TeXy approach. – TeXnician Apr 21 '17 at 15:17

expl3 is written in tex so is limited to the facilities that tex provides so you can not do this unless you use -shell-escape or luatex.

For example this will typeset a list of tex files in the current directory if that is what ls *.tex generates in your operating system. (probably dir *.tex in windows)

\documentclass{article}

\begin{document}

\def\zzpar{\par}

\openin\zzz="|ls *tex"
\loop
\ifx\tmp\zzpar\else
\par[\expandafter\detokenize\expandafter{\tmp}]%
\fi
\ifeof\zzz\else
\repeat

\end{document}


If I run it I get 105 pages looking like

• How would a version with --shell-escape look like (command line tools or what do you mean)? – TeXnician Apr 21 '17 at 16:05
• @TeXnician added an example – David Carlisle Apr 21 '17 at 16:17
• With miktex you need (only) --enable-pipes and the command should be dir /w/B *.tex (and it stopped for me after 6 pages with a runaway argument: my test folder is a garbage heap -- no orderly numbered zz or yy files -- and sometimes in the dark ages I managed to create a file {foo.tex ...) – Ulrike Fischer Apr 22 '17 at 16:21
• @UlrikeFischer Thanks, it really works well. – TeXnician Apr 22 '17 at 16:54
• @DavidCarlisle I think I'll accept it again as it is now real multi-platform. – TeXnician Apr 22 '17 at 16:54

With TeX Live 2017 an enhanced version of TeXOSQuery will be shipped, which also runs in the restricted shell:

\documentclass{article}
\usepackage{texosquery}

\TeXOSQuery{\result}{-i ; .}

\show\result

\stop


This outputs

> \result=macro:

You can then use standard methods for splitting the \result macro into components.
• @UlrikeFischer You have to use a personal texosquery.cfg file, see tug.org/pipermail/tex-live/2017-April/039884.html – egreg Apr 22 '17 at 16:46