4

Is there a really sophisticated method to include all files which name match specific criteria (for example all .cpp, .h files) for given path to directory?

\documentclass{article}
\begin{document}    
    \lstAllFromDirectory{directory}{*.cpp *.h}
\end{document}

What approach should I use in order to get result like that?

  • 1
    I would use simple command line techniques to create a file with the directory listing and then input that file into the latex document. Not "sophisticated", perhaps, but workable. – Dan Nov 29 '16 at 21:13
5

Platform independent approach using texosquery (requires Java):

\documentclass{article}

\usepackage{texosquery}

\begin{document}

\TeXOSQueryFilterFileList{\result}{,}{.*\string\.(cpp|h)}{.}

\makeatletter
\ifx\result\empty
 Query Failed!
\else
\@for\thisfile:=\result\do{%
  File: \texttt{\thisfile}.\par
}
\fi
\makeatother

\end{document}

It needs the shell escape. If the file is called test.tex then:

pdflatex -shell-escape test

With MikTeX, you also need the piped shell escape enabled (--enable-pipes).

There are three variants of texosquery:

  1. texosquery-jre5: requires at least Java 5, but doesn't have the full set of features and is less secure;
  2. texosquery (default): requires at least Java 7, has almost the full set of features, but certain file operations have restrictions;
  3. texosquery-jre8: requires at least Java 8, has the full set of features, but certain file operations have restrictions, and is now on TeX Live's list of trusted applications.

So if you have Java 8 and texosquery-jre8 is on the restricted list (which it is for TL2017) then you can use the restricted shell escape.

To switch from the default texosquery to texosquery-jre8, you need to edit the texosquery.cfg file. The simplest method is to copy the installed texosquery.cfg to your local or home TEXMF tree and edit it from there. That way it won't be overwritten when the distribution is updated.

Set \TeXOSInvokerName to the required application:

\def\TeXOSInvokerName{texosquery-jre8}

Uncomment the following line to allow the application to be run in restricted mode:

\TeXOSQueryAllowRestricted

(Only do this if the application is actually on the restricted list.) Now you can just do (with TeX Live):

pdflatex test

or with MikTeX:

pdflatex --enable-pipes test

Also works with plain eTeX:

\input texosquery

\TeXOSQueryFilterFileList{\result}{,}{.*\string\.(cpp|h)}{.}    

\ifx\result\empty
 Query Failed!
\else
 % iterate over comma-separated list stored in \result using your preferred method
\fi

\bye
| improve this answer | |
  • This is pdfTeX, Version 3.14159265-2.6-1.40.17 (MiKTeX 2.9) entering extended mode (code.tex LaTeX2e <2016/03/31> patch level 3 Babel <3.9r> and hyphenation patterns for 75 language(s) loaded. ("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\article.cls" Document Class: article 2014/09/29 v1.4h Standard LaTeX document class ("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\base\size10.clo")) ("C:\Program Files (x86)\MiKTeX 2.9\tex\latex\texosquery\texosquery.sty" ! LaTeX Error: File `texosquery.tex' not found. – Mr.Nimelo Nov 29 '16 at 21:26
  • @Mr.Nimelo Sounds like something's wrong with your installation of texosquery. You should have both /tex/generic/texosquery/texosquery.tex and /tex/latex/texosquery/texosquery.sty. – Nicola Talbot Nov 29 '16 at 21:31
  • How can i fix installation? I tried to uninstall -> install again – Mr.Nimelo Nov 29 '16 at 21:42
  • Can you check if C:\Program Files (x86)\MiKTeX 2.9\tex\generic\texosquery\texosquery.tex exists? If it's not there then that sounds like there's a problem with the way MikTeX installs it. You can manually create the texosquery.tex file. Download texosquery.dtx and texosquery.ins and do pdflatex texosquery.ins. – Nicola Talbot Nov 29 '16 at 22:09
  • Well it still doesn't work properly. My distribution of TeX installed this package inside floowing directory C:\Program Files (x86)\MiKTeX 2.9\tex\latex. Is a way to add it manually? – Mr.Nimelo Nov 29 '16 at 23:29

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.