# Generating Sequential File Names with Mathematica

One common task when outputting scientific computation results, figures, etc. is the need to include numbers in a file name. For example, including leading zeros in a name can ensure that the files are ordered sequentially when you list the directory contents. If the numerical values correspond to real numbers (as opposed to integers) you may want to include decimals after the decimal point.

The function below generates a name that starts with the string “T_” and then includes a left and right zero padded number at the end of the file name.

BuildNameForPeriod[period_, n_] :=
Module[{},
"T_" <> StringTake[ToString[NumberForm[period, {n, 2},
]

You can test it with

Table[BuildNameForPeriod[i,6],{i,0,200,5}]

which produces the output

{T_000.00,T_005.00,T_010.00,T_015.00,T_020.00,T_025.00,
T_030.00,T_035.00,T_040.00,T_045.00,T_050.00,T_055.00,
T_060.00,T_065.00,T_070.00,T_075.00,T_080.00,T_085.00,
T_090.00,T_095.00,T_100.00,T_105.00,T_110.00,T_115.00,
T_120.00,T_125.00,T_130.00,T_135.00,T_140.00,T_145.00,
T_150.00,T_155.00,T_160.00,T_165.00,T_170.00,T_175.00,
T_180.00,T_185.00,T_190.00,T_195.00,T_200.00}


Of course there ought to be an easier way (and probably is), but this seems to work.