# How to pass a single file path argument to a macro?

I want to iterate all images in a certain directory and import each image with \includegraphics from within the MainInput.tex. The batch file IterateFiles.bat helps us to create a list of image names in the specified directory.

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


The current implementation of \IterateFiles macro takes 3 arguments. The first 2 arguments are the path (relative to the MainInput.tex) to the directory in which images are saved. The bad thing here is that we need to specify the same path in 2 forms: ..\dirsep Images\dirsep and ../Images/.

% MainInput.tex
\documentclass{article}
\usepackage{graphicx}

{
\catcode\^0
\catcode\\12
^gdef^dirsep{\}
}

\newcount\TotalFiles

\makeatletter
\newcommand{\IterateFiles}[3]{%
\immediate\write18{IterateFiles #1 \jobname\space #3}
\openin\myfile=\jobname.list\relax
\loop
\unless\ifeof\myfile
\filename@parse{\mydata}
\section*{\mydata}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{#2\filename@base}
\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}


My question: How to pass a single file path argument ../Images/ (as opposed two arguments) to the above macro?

-
Shouldn't the windows command shell also parse ../Images/? –  Stephan Lehmke Aug 31 '12 at 16:49
@StephanLehmke: Oh my ghost, Windows command shell can also parse ../Images/. I did not know this. If you convert your comment to answer, I will accept it. –  kiss my armpit Aug 31 '12 at 17:09
This would make your question off topic in TeX.SE ;-) In fact I'm not a windows user at all, so I don't feel confident posting an answer about windows (and I couldn't even test it). So if you did all the work, you'd better self-answer ;-) –  Stephan Lehmke Aug 31 '12 at 17:13

Use the xstring package to replace /s with \s (or vice versa):

\StrSubstitute{../Images}{/}{\dirsep}}

-

Oh my ghost, apparently Windows command shell can also parse ../Images/. I did not know this.

% MainInput.tex
\documentclass{article}
\usepackage{graphicx}

\newcount\TotalFiles

\makeatletter
\newcommand{\IterateFiles}[2]{%
\immediate\write18{IterateFiles #1 \jobname\space #2}
\openin\myfile=\jobname.list\relax
\loop
\unless\ifeof\myfile
\filename@parse{\mydata}
\section*{\mydata}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{#1\filename@base}
\repeat
\closein\myfile
}
\makeatother

\begin{document}
\IterateFiles{../Images/}{jpg png pdf eps}

\section*{Summary}
There are \the\TotalFiles\ files in total.
\end{document}


Warning: There is a hidden bug here when using \filenamb@base instead of \mydata. If there are the same filenames with different extensions, then only one of them will be included. For more detailed solution, see How to trim the end of line character for each line read from an external file?.

-