IfFileExists and filecontents causes an error

I would like to use the filecontents environment in my document. I get a warning

LaTeX Warning: File test.txt' already exists on the system.
Not generating it from this source.


If I run multiple passes of pdfLaTeX on the following

\documentclass{article}
\begin{filecontents}{\jobname.txt}
Hello World
\end{filecontents}
\begin{document}
\input{\jobname.txt}
\end{document}


The warning is obvious, the first pass creates the file, so the subsequent passes cause the warning. I thought I would be able to get rid of the warning with IfFileExists. The problem is that

\documentclass{article}
\IfFileExists{\jobname.txt}{}{
\begin{filecontents}{\jobname.txt}
Hello World
\end{filecontents}
}
\begin{document}
\input{\jobname.txt}
\end{document}


Gives me an error

! LaTeX Error: Missing \begin{document}.


on the first pass and subsequent passes. What am I doing wrong?

-
The message is innocuous. – egreg Feb 13 '12 at 21:46
@egreg I understand that, but I thought I could easily avoid the error. I am more curious why IfFileExists causes the error. – StrongBad Feb 13 '12 at 21:56
The error is not from \IfFileExist directly; LaTeX enters a special state when doing filecontents and this makes it unhappy if the environment is in that position. – egreg Feb 13 '12 at 22:06

You can do with this trick that avoids putting the filecontents environment inside the braces, which is the cause of the error:

\begingroup\newif\ifmy
\IfFileExists{\jobname.txt}{}{\mytrue}
\ifmy
\begin{filecontents}{\jobname.txt}
Hello World
\end{filecontents}
\fi\endgroup

-
\ifv@, like many other kernel booleans, isn't used by the filecontents environment. – Ahmed Musa Feb 14 '12 at 1:22
Thanks for this. I don't know if I ever would have figured out, on my own, that adding the filecontents environment to a group causes problems. How did you know when LaTeX is in a "special" state? – StrongBad Feb 14 '12 at 9:19
@DanielE.Shub LaTeX is in such a special state because it may have to write out the file, so it does some tricks with the line terminator. – egreg Feb 14 '12 at 9:22

This may not be entirely what you're after since your question was more about "how to" rather than "just get it done". However, in the question of interest, here one way of overwriting an existing file and not worrying about the overwrite warning issued by LaTeX:

\documentclass{article}
\usepackage{filecontents}% http://ctan.org/pkg/filecontents
\usepackage{silence}% http://ctan.org/pkg/silence
\WarningFilter{latex}{Overwriting file}% Remove LaTeX warnings starting with "Overwriting file"
\begin{filecontents*}{\jobname.txt}
Hello World
\end{filecontents*}
\begin{document}
\input{\jobname.txt}
\end{document}​


The filecontents package provides filecontents* which overwrites a file if it already exists. The silence package gobbles warnings from LaTeX starting with "Overwriting file" via

\WarningFilter{latex}{Overwriting file}


The writing, even though its performed using filecontents, still is a LaTeX-related warning.

-
Werner, that’s only partly true: The filecontents package itself is responsible for the opportunity of overwriting existing files. The filecontents* environment does not print out the internal preamble which is prepended if one uses the environment filecontents. – Speravir Feb 13 '12 at 22:45
@Speravir: This is true, although the "internal preamble" consists only of comments. – Werner Feb 14 '12 at 6:52
While not exactly what I was looking for, the silence` package seems handy for when you know you are going to get warnings that you do not care about.. – StrongBad Feb 14 '12 at 9:17