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 lot of data sets combined in a big list where each data set comprises one first level sub-list. Each data set must be saved into a separate file. The names for the files must automatically assigned during export.

How to export different files at the same time?

The basic form of Export is Export["filename.ext", expr], I have not been able to adapt this basic for a sequence of file names, because I don't know how modify the string "filename.ext" to include a sequence number. How could I do that?

I want to generate ten file names using a loop to export the data automatically. I was thinking maybe an expression like

Do[Export["filename[[n]].txt", data[[n]]], {n, 1, 10}]

where the filename is a list {name_1, name_2, name_3, .., name_10}

share|improve this question
Do[Export["filename" <> ToString[n]<>".txt", data[[n]]], {n, 1, 10}] – ciao May 22 '14 at 2:52
@rasher Thank you very much for your instant reply! It is very useful! – qfzklm May 22 '14 at 2:59
Or you can also check the second part of this. – Öskå May 22 '14 at 9:43
You may also want to use IntegerString instead of ToString to add leading zeros. Check the documentation. – Theo Tiger May 22 '14 at 17:56

In version 10, you can use StringTemplate:

Export[StringTemplate["filename-`1`.txt"][#], data[[#]]] & /@ Range@10

I also like to use IntegerString to get numbers with leading zeros. In this case, base 10 and 3 digits.

Export[StringTemplate["filename-`1`.txt"]@IntegerString[#, 10, 3], data[[#]]] & /@ Range@10
share|improve this answer

As rasher wrote in a comment, you need to use some string manipulation in your Export expression to generate the file name on the fly.

Do[Export["filename" <> ToString[n] <> ".txt", data[[n]]], {n, 1, 10}]
share|improve this answer

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.