4

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?

TeXnician
  • 33,589

2 Answers2

4

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:
->dl.log;dl.tex;install-tl;LICENSE.CTAN;LICENSE.TL;osq.log;osq.tex;qrp.log;qrp.
pdf;qrp.tex;query.aux;query.log;query.pdf;query.tex;rand.log;rand.pdf;rand.tex;
release-texlive.txt;texmf-dist;texnician.log;texnician.tex;texosquery.cfg;texpu
t.log;tlpkg;tt.aux;tt.idx;tt.ilg;tt.ind;tt.log;tt.pdf;tt.tex.

You can then use standard methods for splitting the \result macro into components.

There are restrictions on what directories you can access to, for security reasons, but child directories of the working directory should be safe.

egreg
  • 1,121,712
3

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}

\newread\zzz

\begin{document}

\def\zzpar{\par}

\openin\zzz="|ls *tex"
\loop
\read\zzz to \tmp
\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

enter image description here

David Carlisle
  • 757,742