The following MWE is the simplified version of my project. But it is complete enough to produce the problem. If you disable (comment) the looping parts, compilation succeed. Otherwise you will get compilation error. What makes a loop inside \IfFileExists fail?
\documentclass{book}
\input{repeat}
\def\Foo{%
\IfFileExists{\jobname.tex}{%
\repeat\for{x}\by{0}%
\until{\ifnum\x>10}%
\do{\advance\x by 1}%
}%
{%
File not found.
}%
}
\begin{document}
\Foo
\end{document}
This is pdfTeX, Version 3.1415926-2.3-1.40.12 (Web2C 2011)
restricted \write18 enabled.
entering extended mode
(./dj.tex
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman-x-2011-07-01, ngerman-x-2011-07-01, afrikaans, ancientgreek, ibycus, arabi
c, armenian, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, danis
h, dutch, ukenglish, usenglishmax, esperanto, estonian, ethiopic, farsi, finnis
h, french, friulan, galician, german, ngerman, swissgerman, monogreek, greek, h
ungarian, icelandic, assamese, bengali, gujarati, hindi, kannada, malayalam, ma
rathi, oriya, panjabi, tamil, telugu, indonesian, interlingua, irish, italian,
kurmanji, lao, latin, latvian, lithuanian, mongolian, mongolianlmc, bokmal, nyn
orsk, polish, portuguese, romanian, romansh, russian, sanskrit, serbian, serbia
nc, slovak, slovenian, spanish, swedish, turkish, turkmen, ukrainian, uppersorb
ian, welsh, loaded.
(c:/texlive/2011/texmf-dist/tex/latex/base/book.cls
Document Class: book 2007/10/19 v1.4h Standard LaTeX document class
(c:/texlive/2011/texmf-dist/tex/latex/base/bk10.clo))
(c:/texlive/2011/texmf-dist/tex/generic/eijkhout/repeat.tex
Loading loop macro, version 0.93a) (./dj.aux))
! Incomplete \ifeof; all text was ignored after line 16.
<inserted text>
\fi
<*> dj.tex
?
! Emergency stop.
<inserted text>
\fi
<*> dj.tex
! ==> Fatal error occurred, no output PDF file produced!
Transcript written on dj.log.
\repeat, which is central in repeat file is defined in the LaTeX kernel as\fi, which allows Knuth's (and LaTeX's)\loopto be nested in a conditional. Note: "nested in a conditional" doesn't mean nesting\loopin itself. – Ahmed Musa Jul 05 '12 at 17:44