\documentclass{article}
\usepackage{catchfile}
\usepackage{listings} % Used only for displaying files
\lstset{numbers=left, numberstyle=\tiny, numbersep=5pt,
basicstyle=\ttfamily, language={}, frame=single}
\begin{filecontents}{filetocatchA.tex}
This is some content.
\end{filecontents}
\begin{filecontents*}{filetocatchB.tex}
% This is some comment
% This is some comment
%
\end{filecontents*}
\begin{filecontents}{filetocatchC.tex}
\newbox\mybox
\setbox\mybox=\vbox{%
\hbox{Hello, world!}%
\hbox{Hello, world!}%
\hbox{Hello, world!}%
}%
\wd\mybox=0pt %
\ht\mybox=0pt %
\dp\mybox=0pt %
\box\mybox
\end{filecontents}
\begin{filecontents}{filetocatchD.tex}
\begin{figure}[h]
This is a figure.
\caption{This is the figure's caption.}
\end{figure}
\end{filecontents}
\begin{filecontents}{filetocatchE.tex}
{\def\empty{}\empty\empty\empty}%
\end{filecontents}
\begin{filecontents}{filetocatchF.tex}
}}}}{{{
Unbalanced braces.
\end{filecontents}
\makeatletter
%%-----------------------------------------------------------------------------
%% Check whether argument contains no tokens/is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked contains no tokens/is
%% empty>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked contains some tokens/
%% is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
%%
%% The way in which this macro works is explained in my answer to the
%% TeX-LaTeX-StackExchange-Question "Expandable test for an empty token
%% list—methods, performance, and robustness" at
%% <https://tex.stackexchange.com/a/522506>
%% In that answer it is named \CheckWhetherEmpty.
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter@secondoftwo\string{\expandafter
@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
@secondoftwo\string}\expandafter@firstoftwo\expandafter{\expandafter
@secondoftwo\string}@firstoftwo\expandafter{} @secondoftwo}%
{@firstoftwo\expandafter{} @firstoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether argument is blank (empty/no tokens at all or only
%% explicit space tokens (character code 32/category code 10)):
%%-----------------------------------------------------------------------------
%% -- Take advantage of the fact that TeX discards space tokens when
%% "fetching" _un_delimited arguments: --
%% \UD@CheckWhetherBlank{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that
%% argument which is to be checked is blank>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not blank>}%
\newcommand\UD@CheckWhetherBlank[1]{%
\romannumeral\expandafter\expandafter\expandafter@secondoftwo
\expandafter\UD@CheckWhetherNull\expandafter{@firstoftwo#1{}.}%
}%
\newcommand{\isFileEmpty}[1]{%
\begingroup
\CatchFileDef{\filecontent}{#1}{%
\let\do@makeother
\dospecials
\catcode\%=14 % \catcode\ =10 %
\catcode`^^I=12 %
\endlinechar=-1 %
}%
\expandafter\endgroup
\expandafter\UD@CheckWhetherBlank
\expandafter{\filecontent}%
}%
\makeatother
\parindent=0ex
\parskip=\baselineskip
\begin{document}
This is the content of \verb|filetocatchA.tex|:
\lstinputlisting{filetocatchA.tex}
This is the result of the empty-test:
\isFileEmpty{filetocatchA.tex}{File is empty!}{File is not empty!}
\leavevmode\hrule height .5\ht\strutbox\hfill
This is the content of \verb|filetocatchB.tex|:
\lstinputlisting{filetocatchB.tex}
This is the result of the empty-test:
\isFileEmpty{filetocatchB.tex}{File is empty!}{File is not empty!}
\leavevmode\hrule height .5\ht\strutbox\hfill
This is the content of \verb|filetocatchC.tex|:
\lstinputlisting{filetocatchC.tex}
This is the result of the empty-test:
\isFileEmpty{filetocatchC.tex}{File is empty!}{File is not empty!}
\leavevmode\hrule height .5\ht\strutbox\hfill
This is the content of \verb|filetocatchD.tex|:
\lstinputlisting{filetocatchD.tex}
This is the result of the empty-test:
\isFileEmpty{filetocatchD.tex}{File is empty!}{File is not empty!}
\leavevmode\hrule height .5\ht\strutbox\hfill
This is the content of \verb|filetocatchE.tex|:
\lstinputlisting{filetocatchE.tex}
This is the result of the empty-test:
\isFileEmpty{filetocatchE.tex}{File is empty!}{File is not empty!}
\leavevmode\hrule height .5\ht\strutbox\hfill
This is the content of \verb|filetocatchF.tex|:
\lstinputlisting{filetocatchF.tex}
This is the result of the empty-test:
\isFileEmpty{filetocatchF.tex}{File is empty!}{File is not empty!}
\end{document}
\isFileEmptymacro is going to take\expandafteras its argument. Try putting\expandafterbeforeisFileEmptyas well. Also, since you have the contents of the file in\filecontent, you can use the methods here https://tex.stackexchange.com/questions/53068/how-to-check-if-a-macro-value-is-empty-or-will-not-create-text-with-plain-tex-co to check for contents. Your solution will blow up if, e.g., there's a paragraph break in the input file. – Don Hosek Jul 10 '20 at 20:28\parin restricted horizontal mode does nothing. However there are several other things that may go wrong if tried in restricted horizontal mode. – egreg Jul 11 '20 at 09:19\CatchFileDef? Files with#can be a problem for\CatchFileDef, too. – Ulrich Diez Jul 11 '20 at 17:57\ifthenelse{\expandafter\isFileEmpty\expandafter{\filecontent }}does not seem to have an effect – rmrf Jul 13 '20 at 13:38\CatchFileDef(I discovered while trying to make this work), I have been testing with#comments and have been successful so far; what issues are you aware of with#and\CatchFileDef? – rmrf Jul 13 '20 at 13:46#is not for comments in LaTeX but is for denoting macro parameters. But you are right:\CatchFileDeftreats#correctly. I confused this with some other package (everyhook) where this currently is not the case. I'm sorry about my statement about#and\CatchFileDef- that statement was my mistake. – Ulrich Diez Jul 13 '20 at 20:03