
Ich möchte alle Bilder in einem bestimmten Verzeichnis durchlaufen und jedes Bild \includegraphics
aus dem Verzeichnis importieren MainInput.tex
. Die Batchdatei IterateFiles.bat
hilft uns, eine Liste mit Bildnamen im angegebenen Verzeichnis zu erstellen.
rem IterateFiles.bat
echo off
rem %1 represents the path (relative to the main input file) to the files to be iterated
rem %2 represents the output file name
rem the remaining args represent the extensio of file to be iterated
set curdir=%CD%
cd %1
shift
set output=%curdir%\%1.list
copy nul %output%
shift
:loop
if "%1"=="" goto :eof
dir /b *.%1 >> %output%
shift
goto :loop
Die aktuelle Implementierung des \IterateFiles
Makros benötigt 3 Argumente. Die ersten beiden Argumente sind der Pfad (relativ zu MainInput.tex
) zum Verzeichnis, in dem die Bilder gespeichert werden. Das Schlimme daran ist, dass wir denselben Pfad in 2 Formen angeben müssen: ..\dirsep Images\dirsep
und ../Images/
.
% MainInput.tex
\documentclass{article}
\usepackage{graphicx}
{
\catcode`\^0
\catcode`\\12
^gdef^dirsep{\}
}
\newread\myfile
\newcount\TotalFiles
\makeatletter
\newcommand{\IterateFiles}[3]{%
\immediate\write18{IterateFiles #1 \jobname\space #3}
\openin\myfile=\jobname.list\relax
\loop
\read\myfile to \mydata
\unless\ifeof\myfile
\filename@parse{\mydata}
\section*{\mydata}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{#2\filename@base}
\advance\TotalFiles1\relax
\repeat
\closein\myfile
}
\makeatother
\begin{document}
\IterateFiles{..\dirsep Images\dirsep}{../Images/}{jpg png pdf eps}
\section*{Summary}
There are \the\TotalFiles\ files in total.
\end{document}
Meine Frage: Wie übergebe ich dem obigen Makro ein einzelnes Dateipfadargument ../Images/
(anstatt zwei Argumenten)?
Antwort1
Verwenden Sie das xstring
Paket, um /
s durch \
s zu ersetzen (oder umgekehrt):
\StrSubstitute{../Images}{/}{\dirsep}}
Antwort2
Oh mein Gott, anscheinend kann die Windows-Eingabeaufforderung auch analysieren ../Images/
. Das wusste ich nicht.
% MainInput.tex
\documentclass{article}
\usepackage{graphicx}
\newread\myfile
\newcount\TotalFiles
\makeatletter
\newcommand{\IterateFiles}[2]{%
\immediate\write18{IterateFiles #1 \jobname\space #2}
\openin\myfile=\jobname.list\relax
\loop
\read\myfile to \mydata
\unless\ifeof\myfile
\filename@parse{\mydata}
\section*{\mydata}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{#1\filename@base}
\advance\TotalFiles1\relax
\repeat
\closein\myfile
}
\makeatother
\begin{document}
\IterateFiles{../Images/}{jpg png pdf eps}
\section*{Summary}
There are \the\TotalFiles\ files in total.
\end{document}
\filenamb@base
Achtung: Bei der Verwendung von anstelle von gibt es einen versteckten Fehler \mydata
. Wenn es dieselben Dateinamen mit unterschiedlichen Erweiterungen gibt, wird nur einer davon berücksichtigt. Eine detailliertere Lösung finden Sie unterWie schneidet man das Zeilenendezeichen für jede Zeile ab, die aus einer externen Datei gelesen wird?.