Generalread list of files

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
rec
Posts: 2
Joined: Sat Dec 15, 2012 1:39 pm

read list of files

Post by rec »

How to replace the macro \datatext before \openin is executed?

Code: Select all

\loop{
    \read\indexfile to \datatxt
    {\bf\datatxt}\par
    \advance\cntj by 1
    \openin\datafile = data/\datatxt
In general, how to create dynamic filenames with strings, counters, ...?

data_1.txt
data_2.txt

Create a loop with counter and generate filename with base and counter.

Thx,
REC
Last edited by cgnieder on Sat Dec 15, 2012 3:00 pm, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: read list of files

Post by cgnieder »

Hi and welcome to the LaTeX community!

Can you elaborate on what you're trying to do? It is not very clear from your question as it stands now. (It's also not clear if you're using Plain TeX or LaTeX ...)

Regards
site moderator & package author
rec
Posts: 2
Joined: Sat Dec 15, 2012 1:39 pm

read list of files

Post by rec »

OK, I will try to add more details.

I'm using Latex (MiKTeX and subset pdflatex).

The problem is the inner \openin\datafile = data/\datatxt

The \indexfile contains a filename for each line.
But, how to open the \datafile?

e.g.
data\index.txt:

Code: Select all

a.txt
b.txt
goal is to open:

Code: Select all

\openin\datafile = data/a.txt
\openin\datafile = data/b.txt
...
regards,
rec
Last edited by cgnieder on Sat Dec 15, 2012 4:41 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

read list of files

Post by cgnieder »

I think I understood what you want. Let me try to summarize: in the same directory as your main file you have a folder data. This folder contains the file index.txt and a lot of other files a.txt, b.txt, ... The names of these other files are written in index.txt, one per line. Now you want to loop through the contents of index.txt in order to read in (i.e. input) all the other files.

I have a solution that works for this setting:

Code: Select all

\documentclass{article}
\begin{document}

% make @ letter:
\makeatletter
\newread\myread

% file which contains names of other files:
\def\indexfile{data/index.txt}

% open the file:
\openin\myread=\indexfile

% set endlinechar -1 so it isn't saved to \currentfile
% by \readline; a negative value effectively has the same
% effect as if the end of each line were commented out;
% to keep this change local everything is placed in a group:
\begingroup
  \endlinechar=-1
  % loop until the end of \myread is reached:
  \loop\unless\ifeof\myread
    \readline\myread to \currentfile% save current line to \currentfile
    % now test if \currentfile is empty, if it isn't input the
    % corresponding file:
    \expandafter\ifx\expandafter\relax\currentfile\relax
    \else
      \input{data/\currentfile}\par % the \par is just for this example
    \fi
  \repeat % start again with next line
\endgroup
% close the file again:
\closein\myread

% make @ other again
\makeatother

\end{document}
file.png
file.png (2.71 KiB) Viewed 3376 times
Regards
site moderator & package author
Post Reply