# Writing files out to disk with names that incorporate a parameter value

I have a notebook I run for different values of aspect ratio A all integers except 2.5 and copies its multiple plot results to my hard disc. So when I change A and rerun it, I have to manually change all the file output names to avoid getting the existing results for the previous A overwritten. This is time consuming and mistake prone. What is the syntax for a file name that say has a numerical ending of the current value ot A please?

• "fileName"<>TextString[NumberForm[A,{1,1}]]<>".txt" or A = 2.2;  "fileName" <> StringReplace[#, "." -> "_"] &@ TextString[NumberForm[A, {1, 1}]] <> ".txt" – Rolf Mertig Oct 9 '19 at 14:04
• Nice one, @RolfMertig I always like to make anonymous functions for this type of thing. Something like: "filehead-"<>ToString[#]&<>"-"<>ToString[A]<>".extail"/@Range@10 – CA Trevillian Oct 9 '19 at 14:41
• @simon Why don't you paste your export command? – Rolf Mertig Oct 9 '19 at 16:49
• Try Export[FileNameJoin[{\$HomeDirectory, "eflut3i.tiff"}], out] – Rolf Mertig Oct 10 '19 at 10:57
• Sorry where does the integer A enter in the name to replace my specific value of 3? – simon Oct 10 '19 at 14:37

Regarding the commented example (it is wise to update your post with such things, for future reference) one would do something like the following:

A=3;
out[A_,function_]:=outfunc[A,function];
(*where outfunc[arg, argfunc] is the function based on arg and argfunc one would like to export*)

outexporter[A_,function_,driveletter_]:=outexporter[A,function,driveletter]=Export[ToString[driveletter]<>":\\"<>ToString[function]<>ToString[A]<>"i.tiff", out[A,function],Resolution -> 300]


Hope this helps! Please, let me know if I can explain anything to ya? :D!

• @simon thank you for the kind words! I use this method in my normal workflows where I inevitably use it to make files which have dependencies of 8-10 variables that can change, & it works like a charm. The trick is to weave together various combinations of ToString[varID] and just plain-old varString if you’re setting options with atoms. <> is the shortcut for StringJoin[] and you’ll find that if you keep a consistent data structure, you can make custom Import and Export functions that are evaluated purely by your variable inputs. So I just copy and paste the same inputs for each! – CA Trevillian Oct 21 '19 at 2:12
• @simon nice! So then, this method should work for you! What do you have so far? I’m not sure what you mean by the “internal Mathematica graph name”, nor the “stub output name” but most everything else should be easily achieved with minor modifications to my answer :) – CA Trevillian Oct 21 '19 at 13:45
• @simon I’m not sure what you mean or are asking? You should make re a function of A by simply adding [A_]: between re and =. Then, having done this, you will call re[A] instead of whatever other function you may want to call. So in the end you have re used instead of function when you run outexporter and then outfunc==re. Ie out[A,re] evaluates the output used in outexporter[A,re,C] that gives you the desired effects! – CA Trevillian Oct 21 '19 at 19:57
• – CA Trevillian Oct 21 '19 at 20:43

You may use StringTemplate.

For values of some parameter.

tFilename = StringTemplate["AValueAt1.gif"];
tFilename /@ {1, 2, 2.5, 3}

{"AValueAt1.gif", "AValueAt2.gif", "AValueAt2.5.gif", "AValueAt3.gif"}


You can also include the plot result name.

tFilename2 = StringTemplate["Step1_AValueAt2.gif"];
tFilename2 @@@ {{"One", 1}, {"Two", 2.5}, {"Three", 3}}

{"StepOne_AValueAt1.gif", "StepTwo_AValueAt2.5.gif", "StepThree_AValueAt3.gif"}


StringTemplate can also use named slots with Association.

tFilename3 = StringTemplate["StepStepName_AValueAtValue.gif"];
tFilename3@<|"StepName" -> "Association", "Value" -> 4|>

"StepAssociation_AValueAt4.gif"


Hope this helps.

Credit to CA Trevillian, the short simple answer I wanted which works is

Export["c:/.graph#"<>ToString[A]<>".tiff", out, ImageResolution -> 300]


where <>ToString[A]<> is the sought insertion for the numerical value of A.

• I don't think this is a "thank you" post. I see it as a self-answer where the OP acknowledges the help received from CA Trevillian.. – m_goldberg Oct 24 '19 at 4:09
• Well, my answer was originally accepted but then unaccepted, my intention was to provide a general method with functional approach && I managed that! I hoped OP was able to understand how they could use it, and, I think they landed at a comprehension in the end. Without a functional approach to your project notebook, if one is just setting the A variable, then this would work. You can also, of course, just insert the global A into the format I & others provided! As to which is better, is up to the use case, I think. – CA Trevillian Oct 24 '19 at 13:50