temp = (layer = #;
    layerS = ToString[#];
     layerS]) & /@ {1, 2, 3}

how to get the output of this to actually execute Import command? i.e. go to a directory and import a series of files with indexed filenames ?

  • 1
    $\begingroup$ You've seen ToExpression[]? $\endgroup$ Jun 3, 2013 at 16:12
  • $\begingroup$ I'm not sure if you want to use StringForm if you want to turn these strings into expressions. StringForm only changes the display. See StringJoin. $\endgroup$ Jun 3, 2013 at 16:36
  • 1
    $\begingroup$ @JacobAkkerboom Agreed. Also, if one would insist in using StringForm, the preceding ToString would be fully unnecessary. StringForm doesn't need strings for its slots. $\endgroup$ Jun 3, 2013 at 21:20
  • $\begingroup$ @SjoerdC.deVries I wonder if there is a convenient way at all to work with StringForm. Ah I see I need to edit my answer a bit. $\endgroup$ Jun 3, 2013 at 21:31
  • 1
    $\begingroup$ @JacobAkkerboom You can throw ToString on StringForm. This will turn it into a proper string. $\endgroup$ Jun 3, 2013 at 21:39

2 Answers 2


How about

heldImports = 
 Import["drainCdg_gdat0" <> ToString[#], "Table"] & /@ Hold[1, 2, 3]

Hold[(Import["drainCdg_gdat0" <> ToString[#1], "Table"] &)[1],
(Import["drainCdg_gdat0" <> ToString[#1], "Table"] &)[2],
(Import["drainCdg_gdat0" <> ToString[#1], "Table"] &)[3]]

and then

thisAndThatTable = heldImports[[1]]

Or, maybe more conventionally

heldImports = 
 Import @@@ (Hold @@ ({"drainCdg_gdat0" <> ToString[#], 
        "Table"} & /@ {1, 2, 3}))

Hold[Import["drainCdg_gdat01", "Table"],
Import["drainCdg_gdat02", "Table"],
Import["drainCdg_gdat03", "Table"]]

Note that <> stands for StringJoin.

You could have done something similar with ToExpression. Just don't use StringForm. To see that StringForm is bad for this purpose, note that

StringForm["Import[\"drainCdg_gdat0``\",\"Table\"]", 1] // FullForm


The following is an example using ToExpression

importStrings = 
  "Import[\"drainCdg_gdat0" <> ToString[#] <> "\",\"Table\"]" & /@ {1,
     2, 3}

"Import[\"drainCdg_gdat03\", \"Table\"]"}

import[i_] := ToExpression[importStrings[[i]]];

suchAndSuchTable = import[2];

You can use SetDirectory["dir"] to go to the relevant directory. Or you can prepend the path to the filename using StringJoin (<>), or you can just write "/dir1/dir2/drainCdg_gdat02" of course.


If what you are trying to do is "go to a directory and import a series of files with indexed filenames" there is a simpler way. Say you want to import all the .tif files in a directory with address path.

fileNames = FileNames["*.tif", path];
allFiles = Table[Import[fileNames[[num]]], {num, 1, Length[fileNames]}];

This imports them all at once and they can be indexed as allFiles[[1]], allFiles[[2]], etc. More generally, you can build a list of all the file names and then import the whole list. More succinctly, you can write

allFiles = Import /@ fileNames

to get the same result.


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.