Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a command line that generates lists as output and is running in a loop, so generates, multiple long lists. Each list is to be exported as a .txt file. How can I automate the name generation of the output files. I intend to use the same name as the input file but with the .txt extension in output of course.

  i = 1,i < 3,i++,
  outdata = ReadList[ test[[i]], Record, RecordSeparators -> {{">"},{" "}} ];

How can I automate the export?

share|improve this question
up vote 2 down vote accepted

Something like the following should do:

For[i = 1, i < 3, i++, 
  outdata = ReadList[test[[i]], Record, RecordSeparators -> {{">"}, {" "}}];
  Export[FileBaseName[test[[i]]] <> IntegerString[3, 10, i] <> ".txt", outdata,"List"]

FileBaseName strips the file name in test[[i]] from its extension (if it has any). IntegerString[3, 10, i] generates strings of the form "001", "002", "003" etc. and <> (StringJoin) glues the substrings together.

There is a number of output formats you could use in Export. I chose "List" but you may want to examine the BasicFormats guide page for more.

share|improve this answer
Thank you, but i actually figured out another way! – Aparajita Lahree Mar 12 '13 at 12:45
For[i = 10, i < 20, i++,outdata =ReadList[test[[i]], Record, RecordSeparators -> {{">"}, {" "}}]; prin = "n" <> Riffle /@ (ToString /@ #) & /@ worklist[[i]] <> ".txt"; Export[prin, outdata]] – Aparajita Lahree Mar 12 '13 at 12:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.