# How to get files from a certain folder into my TeX-file without knowing the filenames? [duplicate]

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:

\documentclass{article}
\usepackage{graphicx,caption}
%folder for the figures is /figures/

\begin{document}
%repeat from here for each file in folder figures
\begin{figure}[!htb]
\includegraphics[scale=1]{figures/filename}
\caption{filename}
\end{figure}
\end{document}


## marked as duplicate by egreg floats StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); May 5 '17 at 10:51

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:

\documentclass{article}
\usepackage{graphicx,caption}
\usepackage{texosquery}% requires piped shell escape

\makeatletter

% syntax: \foreachfile{cs}{pattern}{folder}{body}
\newcommand{\foreachfile}[4]{%
\TeXOSQueryFilterFileList{\result}{,}{#2}{#3}%
\ifx\result\empty
Query failed!
\else
\@for#1:=\result\do{#4}%
\fi
}

\makeatother

\begin{document}
\foreachfile{\thisimage}{.*\string\.png}{figures}
{
\begin{figure}[!htb]
\edef\x{\noexpand\includegraphics[scale=1]{figures/\thisimage}}\x
\caption{\thisimage}
\end{figure}
}
\end{document}


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

\def\TeXOSInvokerName{texosquery-jre5}


The line with \TeXOSQueryAllowRestricted must be commented out.

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

\def\TeXOSInvokerName{texosquery}


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

\def\TeXOSInvokerName{texosquery-jre8}


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 – LukasCB May 4 '17 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). – Nicola Talbot May 5 '17 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. – LukasCB May 5 '17 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? – Nicola Talbot May 5 '17 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) – LukasCB May 5 '17 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:

\documentclass[a4paper]{report}
\usepackage{tikz}
\usepackage{graphicx}

\begin{document}
\def \n {3} % change this to the amount of files you need to include.
\foreach \s in {0,...,\n} {%
\begin{figure}
\centering
\includegraphics[scale=0.5]{figures/\s.png}
\caption{This is figure \s}
\end{figure}
}
\end{document}


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

\documentclass{article}
\makeatletter
\def\app@exe{\immediate\write18}
\def\inputAllFiles#1{%
\app@exe{ls #1/*.txt | xargs cat >> \jobname.tmp}%
\InputIfFileExists{\jobname.tmp}{}
\AtEndDocument{\app@exe{rm -f #1/\jobname.tmp}}}
\makeatother
\begin{document}

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

\end{document}


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. – LukasCB May 4 '17 at 21:29
• I added one more example, maybe you can use that? – Ole Anders May 4 '17 at 21:39