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?


1 Answer 1


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.

  • $\begingroup$ Thank you, but i actually figured out another way! $\endgroup$ Mar 12, 2013 at 12:45
  • $\begingroup$ For[i = 10, i < 20, i++,outdata =ReadList[test[[i]], Record, RecordSeparators -> {{">"}, {" "}}]; prin = "n" <> Riffle /@ (ToString /@ #) & /@ worklist[[i]] <> ".txt"; Export[prin, outdata]] $\endgroup$ Mar 12, 2013 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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