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}

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

2 Answers 2


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

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}]

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.