How to iterate through the name of files in a folder
\documentclass{article}
\makeatletter
\def\app@exe{\immediate\write18}
\def\inputAllFiles#1{%
\app@exe{ls #1/*.txt | xargs cat >> \jobname.tmp}%
\InputIfFileExists{\jobname.tmp}{}{}
\AtEndDocument{\app@exe{rm -f #1/\jobname.tmp}}}
\makeatother
\begin{document}
\inputAllFiles{.}% from the current dir
\end{document}
Not really difficult. It is only an appetizer of how it can be done. This one only reads files *.txt. You have to run it with pdflatex -shell-escape test
If there is some sort of naming convention that is used for the files than this can be done as in Can i automatically load chapters and sections based on the filesystem?
\documentclass{article}
\newcommand*{\MaxNumOfChapters}{10}% Adjust these two settings for your needs.
\newcommand*{\MaxNumOfSections}{6}%
\usepackage{pgffor}%
\begin{document}
\foreach \c in {1,2,...,\MaxNumOfChapters}{%
\foreach \s in {1,2,...,\MaxNumOfSections}{%
\IfFileExists{Chapter\c/Section\s} {%
\input{Chapter\c/Section\s}%
}{%
% files does not exist, so nothing to do
}%
}%
}%
\end{document}
This assumes that there is a directory for each chapter named Chapter<i>
directory contains files named Section<n>
. Should be able to customize this depending on your specific case.
This should work across different operating systems.
If there is not specfic naming convention, you can adjust this to process a list of files generated by your C# program. For example, if the C# program can generate ListOfFiles.tex
as
\newcommand*{\ListOfFiles}{%
Chapter1/Section1,
Chapter1/Section2,
Chapter1/Section3,
Chapter2/Section1,
Chapter2/Section2
}%
then you can process this as:
\documentclass{article}%
\usepackage{pgffor}%
\input{ListOfFiles}%
\begin{document}%
\foreach \c in \ListOfFiles {%
\input{\c}%
}%
\end{document}
A LuaTeX solution:
the TeX driver:
\directlua{dofile("inputall.lua")}
\bye
and the Lua input file inputall.lua
:
function dirtree(dir)
if string.sub(dir, -1) == "/" then
dir=string.sub(dir, 1, -2)
end
local function yieldtree(dir)
for entry in lfs.dir(dir) do
if not entry:match("^%.") then
entry=dir.."/"..entry
if lfs.isdir(entry) then
yieldtree(entry)
else
coroutine.yield(entry)
end
end
end
end
return coroutine.wrap(function() yieldtree(dir) end)
end
for i in dirtree(lfs.currentdir()) do
local filename = i:gsub(".*/([^/]+)$","%1")
tex.sprint("\\input " .. filename .. " ")
end
This recurses down a directory tree and inputs all the file found. (Found the dirtree iterator in the Lua users wiki.)