# Macro to create new .tex files

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}
\Ans1 \FPsv{a*b} \eAns
\Ans0 \FPsv{-a*b} \eAns   \eFreeze
\Ans0 \a \eAns  \eFreeze
\Ans0 \b \eAns
}

\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}
\Ans0 $\FPsv{a}i$ \eAns
\Ans0 $\pm\FPsv{a}i$ \eAns
\Ans1 $\pm\FPsv{a}$ \eAns
\Ans0 $\FPsv{a}$ \eAns
}

\end{test2}

\end{document}

It's not entirely clear what you want but perhaps this.

You don't need separate output streams, you can reuse the same one for all the files.

This defines a command that takes a comma separated list of environment names.

\documentclass{article}
\usepackage{environ}

\newwrite\myexport

\makeatletter
\def\myenvs#1{%
\@for\mytmp:=#1\do{%
\show\mytmp
\expandafter\myenv\expandafter{\mytmp}%
}}

\def\myenv#1{\NewEnviron{#1}{%
\toks@=\expandafter{\BODY}%
\immediate\openout\myexport=\jobname-export-#1.tex %
\immediate\write\myexport{\the\toks@}}%
\immediate\closeout\myexport}%

\makeatother

\myenvs{test1,test2}

\begin{document}

\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}
\Ans1 \FPsv{a*b} \eAns
\Ans0 \FPsv{-a*b} \eAns   \eFreeze
\Ans0 \a \eAns  \eFreeze
\Ans0 \b \eAns
}

\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}
\Ans0 $\FPsv{a}i$ \eAns
\Ans0 $\pm\FPsv{a}i$ \eAns
\Ans1 $\pm\FPsv{a}$ \eAns
\Ans0 $\FPsv{a}$ \eAns