Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have various functions that export Excel and text files, and need to have them named with a name based on the content of some variables in the function and the time and date. How can this be done with Mathematica?

share|improve this question
add comment

2 Answers

up vote 13 down vote accepted

In the answer linked here, I did the following:

out = FileNameJoin @ {$TemporaryDirectory, "MathematicaOutput" <> ToString /@ Date[] <> ".rtf"}

/tmp/MathematicaOutput2012519111731.900549.rtf

Then you would say Export[out, ...].

If you want to have the date in a more readable and less detailed form, you could use this for the name:

"file-" <> Riffle[ToString /@ Date[][[;;3]], "-"] <> ".pdf"

file-2012-5-19.pdf

And of course, the content of any other variable could be inserted in the file name using ToString if needed. If you need a different separator between the data components, the Riffle command can be modified accordingly, by changing the second argument to another string that is allowed in filenames on your platform.

The FileNameJoin command is only needed if you'd like to create not just file names but directory structures in a cross-platform fashion.

share|improve this answer
    
Thank you! Excellent answer! –  R Hall May 19 '12 at 18:31
add comment

Here's a variation on Jens' answer:

"file-" <> DateString[Riffle[{"Year", "Month", "Day"}, "-"]] <> ".pdf"

(*
==> "file-2012-05-19.pdf"
*)

which has the advantage of automatically padding the month and day with zeros so that the file names will sort nicely.

share|improve this answer
    
nice and clean! Thanks! –  R Hall May 20 '12 at 0:46
1  
This answer is shorter without Riffle: "file-" <> DateString[{"Year", "-", "Month", "-", "Day"}] <> ".pdf" –  Mr.Wizard May 20 '12 at 16:38
add comment

Your Answer

 
discard

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.