I want to make a macro to import all TeX input files in a directory in which the input file containing the macro exists. When reading an external file, TeX appends a blank line to the last iteration.
In the following code I use a bad algorithm because there are two loops. The first one is used to find the total file, and the other one is used to do the main job.
\documentclass{article}
\usepackage{listings}
\lstset{language={[LaTeX]TeX},breaklines=true}
\def\X
{%
\immediate\write18{cmd /c dir /b *.tex > \jobname-temp.txt}%
\newread\myfile
\openin\myfile=\jobname-temp.txt
\newcount\counter
\counter=0
\loop
\unless\ifeof\myfile
\read\myfile to\mydata
\advance \counter by 1
\repeat
\closein\myfile
\openin\myfile=\jobname-temp.txt
\advance \counter by -1
\loop
\ifnum \counter > 0
\read\myfile to\mydata
\section\mydata
\lstinputlisting{"\mydata"}\newpage
\advance \counter by -1
\repeat
\closein\myfile
}
\begin{document}
\X
\end{document}
I want to use a single loop to increase the performance.
How to detect a blank line inserted by TeX when reading an external file line by line?