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)





\openin\zzz="|ls *tex"
\read\zzz to \tmp


If I run it I get 105 pages looking like

enter image description here

  • 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:


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



This outputs

> \result=macro:

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.

  • For me (on windows) \result is empty when I don't use --shell-escape. With -shell-escape it works with tl17, but not with miktex. – Ulrike Fischer Apr 22 '17 at 16:34
  • @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
  • Ah. I had seen this but forgotten. – Ulrike Fischer Apr 22 '17 at 16:54
  • Accepted David Carlisle's answer again, since it uses only shell-escape (no Java) and is "portable" now. – TeXnician Apr 22 '17 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.