[Tex/LaTex] How to pass a single file path argument to a macro

macros

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{\}
}

\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}

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

Best Answer

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

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