Conditional list of listings - lstlistoflistings only if listings are present

A solution using the totalcount package:

\usepackage[figure,table,lstlisting]{totalcount}

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

The complete example document:

\documentclass[listof=totoc]{scrreprt}

\usepackage[latin1]{inputenx}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{etoolbox}
\usepackage{graphicx}
\usepackage{listings}
\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}

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}

Opposite to the solutions given by the other answers this solution is actually based on the counter usage (instead of the environment usage). So for example this solution works when using \captionof, too, where the other ones fail:

\documentclass[listof=totoc]{scrreprt}

\usepackage{graphicx}

\usepackage{caption}
\usepackage[figure,table]{totalcount}

\newcommand\conditionalLoF{%
  \iftotalfigures\listoffigures\fi}

\begin{document}

\tableofcontents

\conditionalLoF

\vspace{1cm}

A figure:

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

\end{document}

Some additional notes:

The main problem with your question is that there is no perfect solution.

If you add flags to the environments (like the solution from Werner does) you will miss entries to the list of figures/tables. You add a flag to table, longtable, and longtabu, you will miss threeparttable and wraptable and many others. If you add flags to them, too, you will miss \captionof{table} which is totally independent of the environment used. (And you will miss other entries as well.) And a table without \caption will lead to a wrong outcome, too.

If you use the counter approach (like my solution using totalcount) you have problems with code which increments the counter but does not place an entry within the List of Tables, e.g. longtable or longtabu without \caption. You can correct this by decrementing the counter after usage of these environments. But what about a table with \caption[]{...}? This does add a caption, does increment the table counter, but does not produce an entry in the List of Tables either.

A third approach would be patching \caption. But not every environment incrementing the figure or table counter does use \caption, e.g. the hvfigure environment offered by the hvfloat package. And what \caption to patch? There are many different \caption commands, for example a total different code will be used for table and longtable. My caption package tries to patch many flavors of \caption (the one of the LaTeX kernel and of 17 additional packages), but not all of them. So if I would offer a solution within my caption package it would not work with all environments, e.g. environments defined by the algorithm2e package since the caption package is not adapted to this package. (And I don't know longtabu, must take a closer look on the next week-end. And I will offer an alternate solution using my caption package on the next week-end, too.)

So unfortunately you have to choose a solution which fits your needs best. There is no perfect one since any package/environment/command is free to increment the counters, and free to add something to the List of Figures/Tables using its very own code.

Addendum 2016-03-13 As promised a different solution which patches \addcontentsline to handle conditional lists:

\documentclass{article}

\usepackage{caption}
\usepackage{listings,longtable,tabu}

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

\begin{filecontents*}{iflistof.sty}
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{iflistof}[2016/03/12 v0.1 Offers conditional "List of" (AS)]
\providecommand*\iflistof[1]{%
  \@ifundefined{iflistof@#1}\@secondoftwo\@firstoftwo}
\let\ORI@addcontentsline\addcontentsline
\renewcommand*\addcontentsline[2]{%
  \caption@iflistof{iflistof@#2}%
  \ORI@addcontentsline{#1}{#2}}
\newcommand*\caption@iflistof[1]{%
% \@nameuse{#1}{%
    \global\@namedef{#1}{\@secondoftwo}%
    \if@filesw
      \immediate\write\@mainaux{%
        \string\global\string\@namedef{#1}{\string\@firstoftwo}}%
    \fi}%{}}
\endinput
\end{filecontents*}

\usepackage{iflistof}

\begin{document}
\iflistof{figure}{\listoffigures}{}
\iflistof{table}{\listoftables}{}
\iflistof{lstlisting}{\lstlistoflistings}{}
\clearpage

%\begin{table}
%  \caption{A table}
%\end{table}

\begin{longtable}{ll}
\caption{A longtable}\\
\end{longtable}

\begin{longtabu}{ll}
\caption{A longtable}\\
\end{longtabu}

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

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

\end{document}

This seems to be the best approach, but before praising it please play around with it a little bit and report. If it proves well I could add the iflistof package to my "caption package bundle".

See also: https://sourceforge.net/p/latex-caption/tickets/37/


Try (well, my ;-)) package xassoccnt, which provides some of the totcount features and associated counters that are not reset if the figure etc. counter is set to zero, i.e. they are increased continously (unless manipulated directly) -- in conjunction with the Total counter feature the values are stored to the .aux file and are available in the second run.

A slight redefinition of \listof.... checks for the counter value to be greater than zero and loads the relevant original \listof... command. If the counter is zero, then nothing is included.

\documentclass{scrartcl}

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

\NewTotalDocumentCounter{totalfigures}
\NewTotalDocumentCounter{totaltables}
\NewTotalDocumentCounter{totallistings}

\DeclareAssociatedCounters{lstlisting}{totallistings}
\DeclareAssociatedCounters{figure}{totalfigures}
\DeclareAssociatedCounters{table}{totaltables}

\makeatletter
\renewcommand{\TotalValue}[1]{%
  \value{xassoccnt@total@#1}%
}


\let\@@latex@@listoffigures\listoffigures
\let\@@latex@@listoftables\listoftables
\let\@@latex@@lstlistoflistings\lstlistoflistings

\renewcommand{\listoffigures}{%
  \ifnum\TotalValue{totalfigures} > 0 
  \@@latex@@listoffigures%
  \fi
}


\renewcommand{\listoftables}{%
  \ifnum\TotalValue{totaltables} > 0 
  \@@latex@@listoftables%
  \fi
}


\renewcommand{\lstlistoflistings}{%
  \ifnum\TotalValue{totallistings} > 0 
  \@@latex@@lstlistoflistings%
  \fi
}

\makeatother





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

\begin{document}
\tableofcontents

\clearpage

\lstlistoflistings

\listoftables

\listoffigures


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}