I have a folder with couple hundred figures (and only figures in there): How can I import all of them into my tex-file without knowing the filename in a code like \includegraphics{filename} (working with LuaLaTeX?

I would like to have the filename as caption.

General MWE:

%folder for the figures is `/figures/`

%repeat from here for each file in folder `figures`

2 Answers 2


If you have Java installed, you can use texosquery with TeX's piped shell escape. The follow assumes all your image files have the extensions .png:

\usepackage{texosquery}% requires piped shell escape


% syntax: \foreachfile{cs}{pattern}{folder}{body}
    Query failed!



This requires the shell escape enabled. For example:

pdflatex -shell-escape mydoc

(where the file is called mydoc.tex). If you use MiKTeX you also need to use --enable-pipes. If you use LuaTeX with texosquery v1.3 or below, you'll also need to switch off the dry run mode manually with \TeXOSQueryDryRunfalse. This isn't needed for v1.4.

texosquery needs a bit of setting up before use. There should be a configuration file called texosquery.cfg which can be found with

kpsewhich texosquery.cfg

(run from the command line). It's best to copy this file to your TEXMFLOCAL or TEXMFHOME tree to prevent your changes from being overwritten whenever the package is upgraded.

Find out which version of Java you have. (I'm guessing from your comment that you have Java 5.) The version number can be found with

java -version

For example, for me this displays java version "1.8.0_92" which means Java 8.

If you have Java 5 or 6, the texosquery.cfg file needs to contain the line


The line with \TeXOSQueryAllowRestricted must be commented out.

If you have Java 7, the texosquery.cfg file needs to contain the line


and, again, the line with \TeXOSQueryAllowRestricted must be commented out. This is the default configuration.

If you have Java 8, the texosquery.cfg file needs to contain the line


This form of the application has been added to restricted list for TeX Live 2017, so if you have TL2017, you can uncomment \TeXOSQueryAllowRestricted otherwise it needs to remain commented out.

Only the Java 8 version (texosquery-jre8) was evaluated for the restricted list as older versions of Java are considered a security risk.

Note: with texosquery v1.3 and below, you can't use \TeXOSQueryFilterFileList in restricted mode (but it will work with the unrestricted shell escape). Version 1.4 allows it, but with limitations imposed by the security features of the restricted mode.

  • Is a certain version of Java required? I cannot get it working: (|texosquery -f ',' '.*\.png' 'figures' Exception in thread "main" java.lang.UnsupportedClassVersionError: com/dickimawbooks/texosquery/TeXOSQueryJRE7 : Unsupported major.minor version 51.0 May 4, 2017 at 21:21
  • @LukasCB I've updated my answer. It looks like you need to switch to the Java 5 version of texosquery (texosquery-jre5). May 5, 2017 at 10:06
  • My java version is "1.6.0_65", so I inserted \def\TeXOSInvokerName{texosquery-jre5} in the texosquery.cfgwhich I stored at /usr/local/texlive/texmf-local/tex/latex/local/texosquery.cfg. I updated the TeX Live 2017 distribution with sudo texhash, but still no success. May 5, 2017 at 11:12
  • @LukasCB Has the log file message changed from (|texosquery -f to (|texosquery-jre5 -f and is the path to the texosquery.cfg in the log file correct? May 5, 2017 at 12:06
  • Yes to both: TeXOSQuery: texosquery-jre5 -f ',' '.*\.png' '.' [1 and TeXOSQuery: reading configuration file (/usr/local/texlive/texmf-local/tex/latex/local/texosquery.cfg) May 5, 2017 at 19:34

Maybe not exactly how you wanted it, but it can be a start.

Example 1: The following example uses the tikz package:


    \def \n {3} % change this to the amount of files you need to include.
     \foreach \s in {0,...,\n} {%
        \caption{This is figure \s}

It is important to note that figures need to have numeric names.

Example 2: This second example is taken from How to iterate through the name of files in a folder

  \app@exe{ls #1/*.txt | xargs cat >> \jobname.tmp}%
  \AtEndDocument{\app@exe{rm -f #1/\jobname.tmp}}}

\inputAllFiles{figures/.}% from the figures dir 


It is noted that you need to compile with pdflatex -shell-escape test

  • Your solution makes it necessary to have the files named 1, 2, 3 ..... I am looking for a solution which is inserting the existing files instead of inserting files according to a given system. May 4, 2017 at 21:29
  • I added one more example, maybe you can use that?
    – Ole Aldric
    May 4, 2017 at 21:39

Not the answer you're looking for? Browse other questions tagged .