1
$\begingroup$

I have many files stored in the notebook directory, which are called "lista.dat", "listb.dat", ... I would like to import all of them and save them as variables called "lista", "listb",...

So far I have a few things required, but not figured out all of it.

I set my directory to the correct one with

SetDirectory[NotebookDirectory[]];

I get all filenames in that directory with

files=FileNames["*.dat"]

I get the name without extension of a certain file in the files list with

FileBaseName[files[[i]]]

I have no idea how I would create a new variable which is named like that.

$\endgroup$
1

1 Answer 1

1
$\begingroup$
ClearAll[files];
files=FileNames["*.dat"];
ClearAll[Evaluate[FileBaseName /@ files]];
With[
   {vars = Symbol@*FileBaseName /@ files},
   vars = Import/@ files
]
$\endgroup$

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.