The command \IfFileExists works great to test is a file exists. Is there an equivalent to test if a directory exists? \IfFileExists does not seem to work for directories.
\documentclass{article}
\begin{document}
\newcommand*{\ExistingDirName}{./ExistingDir}% This exists
\newcommand*{\NonExistingDirName}{./NonExistingDir}% This does not exist
\newcommand*{\ExistingFileName}{\ExistingDirName/ExistingFile.tex}% This exists
\newcommand*{\NonExistingFileName}{NonExistingFile.tex}% This does not exist
\newcommand*{\CheckExistence}[1]{
\IfFileExists{#1}{
"#1" exists\par
}{
"#1" does NOT exist\par
}
}
\CheckExistence{\ExistingDirName}% incorrect results
\CheckExistence{\NonExistingDirName}% correct result
\bigskip
\CheckExistence{\ExistingFileName}% works
\CheckExistence{\NonExistingFileName}% works
\end{document}
\IfFileExistsuses the TeX facility for reading a file line by line: it tries to open it for line by line reading and issues success or failure. There are two catches: (1) if a file name is given without extension,.texis added; (2) the operating system won't allow to read a directory, which is not a readable file. – egreg Jun 13 '11 at 20:36\immediate\write18{touch \testdir/bogus_file}and then use\IfFileExistson the newly-created bogus_file. And then delete it with another write18. – David Hammen Jun 13 '11 at 22:17