[Tex/LaTex] 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

set output=%curdir%\%1.list
copy nul %output%

if "%1"=="" goto :eof
dir /b *.%1 >> %output%
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



\immediate\write18{IterateFiles #1 \jobname\space #3}
    \read\myfile to \mydata

\IterateFiles{..\dirsep Images\dirsep}{../Images/}{jpg png pdf eps}

There are \the\TotalFiles\ files in total.

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):

Related Question