I have a set of files, e.g. file1.dat, file2.dat,... containing data (list of data points to plot, manipulate...) I want to do the following

file1 = Import["file1.dat"]; 

where variable name file1 is derived from the imported file name. And then do the same for the chosen filenames in the working directory.

Manual this is done by typing the name of the variable (which is equal to the name of the file), Set, Import the same file:


Making it automatic, I tried in this manner: ToExpression@StringDrop[FileNames[][[1]], -4] = Import[FileNames[][[1]]]

Where on the left side of Set I try to make a variable name (dropping last four characters of the filename i.e. ".dat") from the first file in the working directory, and import the file with the same name on the right side of Set. But it is not possible to make the variable name on the left side of Set. I get the following response:

Set::write: Tag ToExpression in ToExpression[AlNFeb4data0d30] is Protected. >>

Thank you for the answers, somehow I could not make them work for my case so far.

  • $\begingroup$ Greetings! Make the most of Mma.SE and take the tour now. Help us to help you, write an excellent question. Edit if improvable, show due diligence, give brief context, include minimal working examples of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ – rhermans Jan 29 '16 at 10:43

You can use Symbol and Formal Symbols.

With[{\[FormalS] = Symbol["x"]}, \[FormalS] = 4];
(* 4 *)

For your case:

filename = "file1";
With[{\[FormalS] = Symbol[filename]}, 
   \[FormalS] = Import[filename <> ".dat"];]

A symbol file1 is created by Symbol and formal s is used to hold it. Then this symbol (file1) is assigned the import. You can then use file1 in the remainder of your code.

Hope this helps.


To see how to extract information from filenames see this question. Notice that indexed variables, as described in the answers to this question may be more advisable than customized names created as in my answer. All that said,

You can get the filenames (JPG in this example)

fn =  FileNames["IMG*.jpg"]
{"IMG_20150417_103421814.jpg", "IMG_20150417_103433212.jpg", \
 "IMG_20150417_103443550.jpg", "IMG_20150417_103454042.jpg", \
 "IMG_20150428_101733676.jpg", "IMG_20150428_101745989.jpg"}

And test how to extract the relevant information from them

   "IMG_" ~~
     date : NumberString ~~
     "_" ~~
     index : NumberString ~~
     ".jpg" :> {date, index}] & /@ fn
{{{"20150417", "103421814"}},
 {{"20150417", "103433212"}}, 
 {{"20150417", "103443550"}},
 {{"20150417", "103454042"}},
 {{"20150428", "101733676"}},
 {{"20150428", "101745989"}}}

Now create the variables using ToExpression and Evaluate

     "pic" <> First[StringCases[file,
        "IMG_" ~~
          date : NumberString ~~
          "_" ~~
          index : NumberString ~~
          ".jpg" :> index]
    ] = Import[file];, {file, fn}]

Mathematica graphics

Much simpler and more advisable would be to do

pictures = Import /@ FileNames["IMG*.jpg"];

Note - The code below lets you access your data from the file name but it does not exactly do what you want.

Lets say you have some data files named {file1.dat,file2.dat,file3.dat}. Keep the notebook with the code below in the same directory.

 aLLfiles = FileNames[NotebookDirectory[] <> "*.dat"];

 pos[i] = First @@ StringPosition[FileNameTake[aLLfiles[[i]]], "."];
 name[i] = StringTake[FileNameTake[aLLfiles[[i]]], pos[i] - 1];
 dfile[name[i]] = Import[aLLfiles[[i]], "Data"];, {i, 1, 

Now you can get your data using the corresponding filename


Not the answer you're looking for? Browse other questions tagged or ask your own question.