I borrowed from Extracting the contents of text in a specified environment into a new file some code that extracts lines from a .tex file and puts it into a new file. The code works well, but I would like to create a loop that will perform this for a bunch of test problems. In the example below, the user names a new environment: test and the macro pulls the text and puts it into a new file. I would like to be able to define multiple environments and use the macro to generate a different file for each.
\documentclass{article}
\usepackage{environ}
\newwrite\myexportone
\newwrite\myexporttwo
\makeatletter
\NewEnviron{test1}{%
\toks@=\expandafter{\BODY}%
\immediate\write\myexportone{\the\toks@}}
\makeatother
\makeatletter
\NewEnviron{test2}{%
\toks@=\expandafter{\BODY}%
\immediate\write\myexporttwo{\the\toks@}}
\makeatother
\begin{document}
\immediate\openout\myexportone=export1.tex
\immediate\openout\myexporttwo=export2.tex
\begin{test1}
\newproblem{
\FPsetpar{a}{-10}{10}[{-1,1}]
\FPsetpar{b}{-10}{10}[{-1,1}]
\item If
\begin{equation*} \a (x \dsign{\b} \b)=\a x+m \end{equation*} what is the value of $m$?
\vspace{10 mm}
\begin{answers}{2}\bChoices[random]
\Ans1 \FPsv{a*b} \eAns
\Ans0 \FPsv{-a*b} \eAns \eFreeze
\Ans0 \a \eAns \eFreeze
\Ans0 \b \eAns
\eChoices\end{answers}
}
\end{test1}
\begin{test2}
\newproblem{
\FPsetpar{a}{1}{10}
\item What is the solution set for $x^2-\FPsv{\a^2}=0$?
\vspace{10 mm}
\begin{answers}{2}\bChoices[random]
\Ans0 $\FPsv{a}i$ \eAns
\Ans0 $\pm\FPsv{a}i$ \eAns
\Ans1 $\pm\FPsv{a}$ \eAns
\Ans0 $\FPsv{a}$ \eAns
\eChoices\end{answers}
}
\end{test2}
\end{document}