From: myers@emx.cc.utexas.edu (Eric Myers)
Newsgroups: comp.text.tex
Subject: Re: input only existing file
Date: 16 Dec 92 21:36:53 GMT
Organization: Center for Relativity, University of Texas

In article <1992Dec14.150513.10159@rcvie.co.at> lan_schn@rcvie.co.at
(Wolfgang Schnabl) writes: 
>Hi.
>I want to include a file into TeX if the file exists. If it doesn't,
>nothing should happen. Is there a possibility to check within TeX,
>if an specific file exists?


  It is possible to do this using \openin and \ifeof, but on some
implementations of TeX this only works if the file is in the "current"
directory (which means you can't conditionally load from some other
directory, like in your TEXINPUTS list, with this method).  But it works
just fine in the standard Unix version you can get from labrea.stanford.edu.

  With that warning in mind, here is what you can do to read foo.tex
only if it exists:

	\newread\Infile 
	\immediate\openin\Infile=foo.tex 
	\ifeof\Infile			% will be EOF if file does not exist
	  \immediate\closein\Infile	% just close it
	\else				% not EOF, so file exists
	  \immediate\closein\Infile	% close it
	  \input foo.tex		% and read it in 
	\fi

  The problem with some implementations of TeX is that even though you
would be able to \input the file, the \openin can't open it, so the
\ifeof is true anyway.  How do you know whether you have this problem?
The simplest way to test is to try it out on null.tex (assuming you
don't have a file by that name in the current directory, but you do have
one on your system, as everyone should).  I've written a little test
program to do this, and it's attached below.

   I would like to conduct a survey of the various implementations of
TeX and how they deal with this simple test.  If you try this out on
your TeX, and you know which TeX you have (including version number!),
could you please e-mail me that information (including the log file, if
necessary) and I will summarize to the net.

Eric Myers		"Frankie say '\relax'"
		
Center for Relativity, Department of Physics, University of Texas at Austin
myers@emx.utexas.edu  |   myers@utaphy.bitnet   |   myers@ut-emx.UUCP
--
% file: openin.tex
%======================================================================*
% Testing how \openin works on your implementation of TeX
%
% Various implementations of TeX on different operating systems treat
% \openin differently.  In the standard Unix distribution from
% labrea.stanford.edu \opeinin will look for the named file in
% the same list of places it looks for files to \input.  This is nice,
% because it lets us check to see if a file exists before trying to
% \input it.  However other implementations, such as OZTeX on the Mac
% and Northlake's version on VMS, look in only the current directory,
% even if they would \input the file from somewhere else.  I don't like
% this, but I've been told that DEK approves of either behaviour.
%
% Use this file to test your version of TeX.  If you get the
% ``Excellent!'' message then you can use this construction to test for 
% whether a file exists before \inputing.  
%
% Eric Myers <myers@newton.slu.edu>     16 December 1992 
%======================================================================*
\def\emsg#1{\immediate\write16{#1}}% write on a line by itself
\newread\Infile
\def\filename{null.tex}% null.tex, nul.tex,  or other ``system'' file
\emsg{}
\emsg{First I will see if I can tell if \filename\space is 
	on your system...}
\emsg{}
\immediate\openin\Infile=\filename \relax
\ifeof\Infile
   \emsg{...Bogus!}
   \emsg{I cannot tell if \filename\space is on this system using %
        \noexpand\openin.}
   \emsg{My test says that I can't \noexpand\openin it}.
\else
   \emsg{...Excellent!}
   \emsg{It looks okay to me.   I can \noexpand\openin \filename}
   \emsg{without an EOF, so I should be able to \noexpand\input it.}
\fi
\immediate\closein\Infile
\emsg{ }
\emsg{Now, in any case, we will simply \noexpand\input the file....}
\input \filename
\emsg{Did that work?}
\emsg{Please e-mail the results to myers@emx.cc.utexas.edu.}
\emsg{}
\end

Newsgroups: comp.text.tex