12.14.9 \IfFileExists & \InputIfFileExists

Synopses:

\IfFileExists{filename}{true-code}{false-code}
\InputIfFileExists{filename}{true-code}{false-code}

\IfFileExists executes true-code if LaTeX finds the file filename or false-code otherwise. In the first case it executing true-code and then inputs the file. Thus the command

\IfFileExists{img.pdf}{%
  \includegraphics{img.pdf}}
  {\typeout{!! img.pdf not found}

will include the graphic img.pdf if it is found and otherwise give a warning.

This command looks for the file in all search paths that LaTeX uses, not only in the current directory. To look only in the current directory do something like \IfFileExists{./filename}{true-code}{false-code}. If you ask for a filename without a .tex extension then LaTeX will first look for the file by appending the .tex; for more on how LaTeX handles file extensions see \input.

\InputIfFileExists is similar, but, as the name states, automatically \inputs filename if it exists. The true-code is executed just before the \input; if the file doesn’t exist, the false-code is executed. An example:

\InputIfFileExists{mypkg.cfg}
  {\PackageInfo{Loading mypkg.cfg for configuration information}}
  {\PackageInfo{No mypkg.cfg found}}

Unofficial LaTeX2e reference manual