# Using Mathematica to batch-produce text files

I need to make a bunch of plaintext files, all of which differ only in a single parameter. The text I want to "copy" or "batch produce" is a little bit complex, since it contains quoted areas and greek letters that I want to be output in their \[letter] format. What I would like to do is to devise a code in which I can input, say,

My house number i is "my house" number i, letter α

And, when run from i=1 to i=4 output four different plaintext files containing

My house number 1 is "my house" number 1, letter \[Alpha]

My house number 2 is "my house" number 2, letter \[Alpha]

My house number 3 is "my house" number 3, letter \[Alpha]

My house number 4 is "my house" number 4, letter \[Alpha]

respectively (i.e, the first output file the first phrase, the second file the second phrase, and so on.)

Jason's advice is very insightful, but I'm having problems with the quotations still. Is there any way I can do what I want to do? If so, how?

Thank you so much for your help

• Put may be useful here. – Yves Klett Apr 29 '16 at 7:26
• Do[ Export["file_" <> IntegerString[n] <> ".txt", "My house number " <> IntegerString[n] <> " is \"my house\" number " <> IntegerString[n]], {n, 5}] – Jason B. Apr 29 '16 at 9:44
• Do[Export["file_" <> IntegerString[n] <> ".txt", "My house number " <> IntegerString[n] <> " is \"my house\" number " <> IntegerString[n] <> " letter \[Alpha]", CharacterEncoding -> "ASCII"], {n, 5}] – Jason B. Apr 29 '16 at 10:16
• Any more changes or can I put this in the answer? :-D – Jason B. Apr 29 '16 at 10:16
• This works, thank you for your time and apologies for the misunderstandings – Alex Apr 29 '16 at 10:39

There are two issues here:

1. How to write a string that contains a quotation within it, when the quotation mark is what signifies to the system when a string begins and ends.
2. How to export strings with special characters, and have them written in their escaped form: \[Alpha] and not α

For the first, you need to construct the string using \" to begin and end the quotation within the string. For the second you need to export using ASCII character encoding, rather than the more modern UTF-8.

This should create the files you describe:

Do[Export["file_" <> IntegerString[n] <> ".txt",
"My house number " <> IntegerString[n] <>
" is \"my house\" number " <> IntegerString[n] <>
" letter α", CharacterEncoding -> "ASCII"], {n, 5}]


Use Write instead of Export

This command

Write["test.txt", "Hello α world \n Goodbye β world"]


produces this output in the file,

"Hello \[Alpha] world \n Goodbye \[Beta] world"

but this command

Export["test2.txt", "Hello α world \n Goodbye β world"]


give this file

Hello α world

Goodbye β world

• Hi Jason, thank you for the very nice and elegant answer. However, I'm having issues when trying to save outputs with non-quoted parts, such as Hello ''World''. Do you know how to have this exact string in the output file? – Alex Apr 29 '16 at 8:58
• Alex, you need to make your question clear from the beginning, to avoid having answers that, while they answer the exact question in the post, don't answer your real problem. In the code you posted, you were writing a string to a file, and you said you wanted to keep the quotes in the string and to keep the special characters in their escaped form. But what do you mean by Hello "World" - is that a string? If not, then Mathematica interprets it as Times[ "World", Hello] – Jason B. Apr 29 '16 at 9:06
• I apologize that the fact that the quotations did not cover the whole text was not clear enough. I have rewritten the question to make more explicit the exact thing that I want – Alex Apr 29 '16 at 9:19
• On a tiny side annotation, if you want UNIX-type newlines instead of Windows-type, one needs to add the option DOSTextFormat -> False to the Export command. – Alex May 1 '16 at 21:21