Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

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
}
share|improve this question

marked as duplicate by Mico, Kurt, Claudio Fiandrino, percusse, Werner Feb 14 '13 at 20:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4  
See here \IfFileExists{file}{then-code}{else-code} \InputIfFileExists{file}{then-code}{else-code} –  Eddy_Em Feb 14 '13 at 13:14
2  
An example invokation of \IfFileExists : tex.stackexchange.com/questions/39982/… –  T. Verron Feb 14 '13 at 13:15
2  
If anything, the other question should be merged into this one. –  Seamus Feb 14 '13 at 18:16

1 Answer 1

up vote 22 down vote accepted

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}
share|improve this answer
2  
good idea making it CW :) –  cmhughes Feb 14 '13 at 16:49
    
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
    
@BiancaLobo Thanks :) –  tohecz Feb 14 '13 at 21:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.