Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
3  
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. –  In PSTricks we trust 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

2 Answers 2

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

\StrSubstitute{../Images}{/}{\dirsep}}
share|improve this answer
up vote 1 down vote accepted

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

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

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?.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.