65

Can I somehow test if a file exists? And can I use this to do only things when it exists? I like to do something like

if(exist('members.csv')) {
 do something
}
lockstep
  • 250,273
DaPhil
  • 3,365

2 Answers2

104

Yes, you can:

\IfFileExists{filename}{true-branch}{false-branch}

Notice that this looks for the file in all search pathes of LaTeX, so not only in the current directory, but in the texmf tree as well. Therefore, you can use it for instance for a "poor man's solution" when a package is missing:

\IfFileExists{upgreek.sty}{\usepackage{upgreek}}{\let\upmu\mu}

If you really want to search only in the current directory, you can do so by saying

\IfFileExists{./filename}{true-branch}{false-branch}
yo'
  • 51,322
  • 2
    To add to @tohecz answer (I don't believe I can edit yet), I believe that if you want to restrict it to just the current directory, you can use this: \IfFileExists{./filename}{true-branch}{false-branch} and the ./ stops it looking in the other places. – Bianca Lobo Feb 14 '13 at 17:56
  • 1
    This seems to have problems with underscores in filenames? – fuenfundachtzig Oct 14 '15 at 13:39
  • @fuenfundachtzig That's quite possible, I didn't check. – yo' Oct 14 '15 at 13:55
  • 5
    What is the package here for \IfFileExists? – Léo Léopold Hertz 준영 Nov 29 '16 at 12:58
  • 1
    @LéoLéopoldHertz준영 LaTeX kernel. No package necessary. – Faheem Mitha May 27 '19 at 09:12
  • You can also test the existence of a file given its fully qualified path: '/full/path/to/file/' on unix, or relative to the parent. \IfFileExists really means 'If I can read the file': \IfFileExists will execute the false branch if a file exist but you are not authorized to read it. – Jérôme LAURENS May 07 '22 at 20:44
8

The question was not limited to LaTeX here but the answer here is only usable in LaTeX. I add other alternatives.

At TeX primitive level:

\openin15=filename   % or \openin15=./filename for current directory only
\ifeof15           ... file does not exist
\else\closein15    ... file exists
\fi

You can allocate the input file descriptor (15 here) by Plain TeX macro \newread. It means tat more common is: \newread\testfile, \openin\tesfile=filename and \ifeof\testfile.

The main point is that TeX allows to do \openin of non-existed file, but it returns true when \ifeof is used before first \read. On the other hand, if the file exists and it is empty, then first \ifeof returns false, first \read defines empty macro and the \ifeof (used after this \read) returns true.

Notice: All TeX engines since 2020 allow alternative syntax \openin15={\filename} which is incompatible with TeX 82 but allows to use spaces in file names.

If you are using OpTeX then there is a macro \isfile for doing this task:

 \isfile{filename}\iffalse ...file doesn't exist...\fi
 or
 \isfile{filename}\iftrue  ...file do exist...\fi
 or
 \isfile{filename}\iftrue  ...file do exist...\else ...file doesn't exist...\fi
wipet
  • 74,238
  • About the remark, I believe \openin15="file name" also work and allow spaces in file name. Not sure about the compatibility. (that having said it means " cannot be used in file name at all) – user202729 May 13 '23 at 03:34