Platform independent approach using texosquery (requires Java):
\documentclass{article}
\usepackage{texosquery}
\begin{document}
\TeXOSQueryFilterFileList{\result}{,}{.*\string\.(cpp|h)}{.}
\makeatletter
\ifx\result\empty
Query Failed!
\else
\@for\thisfile:=\result\do{%
File: \texttt{\thisfile}.\par
}
\fi
\makeatother
\end{document}
It needs the shell escape. If the file is called test.tex then:
pdflatex -shell-escape test
With MikTeX, you also need the piped shell escape enabled (--enable-pipes).
There are three variants of texosquery:
texosquery-jre5: requires at least Java 5, but doesn't have the full set of features and is less secure;
texosquery (default): requires at least Java 7, has almost the full set of features, but certain file operations have restrictions;
texosquery-jre8: requires at least Java 8, has the full set of features, but certain file operations have restrictions, and is now on TeX Live's list of trusted applications.
So if you have Java 8 and texosquery-jre8 is on the restricted list (which it is for TL2017) then you can use the restricted shell escape.
To switch from the default texosquery to texosquery-jre8, you need to edit the texosquery.cfg file. The simplest method is to copy the installed texosquery.cfg to your local or home TEXMF tree and edit it from there. That way it won't be overwritten when the distribution is updated.
Set \TeXOSInvokerName to the required application:
\def\TeXOSInvokerName{texosquery-jre8}
Uncomment the following line to allow the application to be run in restricted mode:
\TeXOSQueryAllowRestricted
(Only do this if the application is actually on the restricted list.) Now you can just do (with TeX Live):
pdflatex test
or with MikTeX:
pdflatex --enable-pipes test
Also works with plain eTeX:
\input texosquery
\TeXOSQueryFilterFileList{\result}{,}{.*\string\.(cpp|h)}{.}
\ifx\result\empty
Query Failed!
\else
% iterate over comma-separated list stored in \result using your preferred method
\fi
\bye