As shown in [this thread](https://tex.stackexchange.com/questions/33193/how-to-determine-whether-a-list-of-figures-is-empty-then-not-show-it-at-all) and [this thread](https://tex.stackexchange.com/questions/234398/list-of-figures-and-tables-when-there-are-no-figures-or-tables) it is possible to conditionally create lists of figures and tables only if these items are used in a document.

I would like to extends this to listings. Everything works fine as long as you use the `lstlisting` environment at least once in the document. Normally, I include code with `\lstinputlisting`. Therefore, the `lstlisting` environment is not used. Thus, the `\AtEndEnvironmet`is never called and the command `\conditionalLoL` is never set to true.

Is there a way modify the definition of `\conditionalLoL` to include `\lstinputlisting` ?

---

Code so far:

```
\documentclass[listof=totoc]{scrreprt}

\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{tabularx}

% https://tex.stackexchange.com/questions/33193/how-to-determine-whether-a-list-of-figures-is-empty-then-not-show-it-at-all
\makeatletter
  % Figure
  \AtEndEnvironment{figure}{\gdef\there@is@a@figure{}}%
  \AtEndDocument{\ifdefined\there@is@a@figure\label{fig:was:used:in:doc}\fi}%
  \newcommand{\conditionalLoF}{\@ifundefined{r@fig:was:used:in:doc}{}{\listoffigures}}%
  % Table
  \AtEndEnvironment{table}{\gdef\there@is@a@table{}}%
  \AtEndDocument{\ifdefined\there@is@a@table\label{tab:was:used:in:doc}\fi}%
  \newcommand{\conditionalLoT}{\@ifundefined{r@tab:was:used:in:doc}{}{\listoftables}}%
  % Listing
  \AtEndEnvironment{lstlisting}{\gdef\there@is@a@listing{}}%
  \AtEndDocument{\ifdefined\there@is@a@listing\label{lst:was:used:in:doc}\fi}%
  \newcommand{\conditionalLoL}{\@ifundefined{r@lst:was:used:in:doc}{}{\lstlistoflistings}}%
  % 
\makeatother

\begin{filecontents*}{command.txt}
Line 1
Line 2
\end{filecontents*}

\begin{document}

\clearpage
\tableofcontents

\conditionalLoF
\let\LaTeXStandardClearpage\clearpage
\let\clearpage\relax
\conditionalLoT
\conditionalLoL
\let\clearpage\LaTeXStandardClearpage % Return to the old definition

\vspace{1cm}

A figure:

\begin{figure}[htbp]
\centering
\includegraphics[width=0.25\linewidth]{example-image-a}
\caption{A picture}
\label{fig:label}
\end{figure}

A table:

\begin{table}[htbp]
\centering
\caption{A table}
\label{tab:label}
\begin{tabularx}{\linewidth}{lX}
test & test test test test test test test test test test test test test test test test test test test test
\end{tabularx}
\end{table}

A listing environment:

% \begin{lstlisting}[label=lst:label1,caption=A listing]
% command
% command
% \end{lstlisting}

A lstinputlisting:

\lstinputlisting[label=lst:label2,caption=Another listing]{command.txt}

\end{document}

```

---

Result with lstlisting environment:

[![with lstlisting](https://i.stack.imgur.com/jFYTg.png)](https://i.stack.imgur.com/jFYTg.png)

---

Result only with listinputlisting:

[![enter image description here](https://i.stack.imgur.com/74DZ2.png)](https://i.stack.imgur.com/74DZ2.png)

Update
------

Using @Axel Sommerfeldt's answer I still got an empty list of tables for my original document. I found out that this is caused by a `longtabu` environment in my documentclass. Following is the MWE with `longtabu` causing an empty list of tables:

```
\documentclass[listof=totoc]{scrreprt}

\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{longtable}
\usepackage{tabu}
\usepackage{tabularx}
\usepackage[figure,table,lstlisting]{totalcount}

\newcommand\conditionalLoF{%
  \iftotalfigures\listoffigures\fi}
\newcommand\conditionalLoT{%
  \iftotaltables\listoftables\fi}
\newcommand\conditionalLoL{%
  \iftotallstlistings\lstlistoflistings\fi}

\begin{filecontents*}{command.txt}
Line 1
Line 2
\end{filecontents*}

\begin{document}

\clearpage
\tableofcontents

\conditionalLoF
\let\LaTeXStandardClearpage\clearpage
\let\clearpage\relax
\conditionalLoT
\conditionalLoL
\let\clearpage\LaTeXStandardClearpage % Return to the old definition

\vspace{1cm}

\newpage

\chapter{Tests}

\section{Figures}

A figure:

\begin{figure}[htbp]
\centering
\includegraphics[width=0.25\linewidth]{example-image-a}
\caption{A picture}
\label{fig:label}
\end{figure}

A non-floating picture:

\includegraphics[width=0.25\linewidth]{example-image-a}

\section{Tables}

% A table:
% 
% \begin{table}[htbp]
% \centering
% \caption{A table}
% \label{tab:label}
% \begin{tabularx}{\linewidth}{lX}
% test & test test test test test test test test test test test test test test test test test test test test
% \end{tabularx}
% \end{table}
% 
% A non-floating table:
% 
% \begin{tabularx}{\linewidth}{lX}
% test & test test test test test test test test test test test test test test test test test test test test
% \end{tabularx}

A non-floating longtabu

\begin{longtabu} to \textwidth {lX}
test & test test test test test test test test test test test test test test test test test test test test
\end{longtabu}

\section{Listings}

A listing environment:

\begin{lstlisting}[label=lst:label1,caption=A listing environment]
command
command
\end{lstlisting}

A lstinputlisting:

\lstinputlisting[label=lst:label2,caption=A lstinputlisting]{command.txt}

\end{document}

```

I had a quick look at the [`tabu`](https://www.ctan.org/pkg/tabu?lang=de) package documentation. But I am not sure why the counter for tables is added to for an non-floating `longtabu` in the [`totalcount`](http://mirror.unl.edu/ctan/macros/latex/contrib/caption/totalcount.pdf) package.

Any ideas?