# Efficiently initialize list of consecutively named variables for file import

Let's say I have the code:

     $runningLogFile0 = "m0.txt";$runningLogFile1 = "m1.txt";
$runningLogFile2 = "m2.txt"; If[FileExistsQ[$runningLogFile0], Get[$runningLogFile0], Print["Error: Noexist"];]; If[FileExistsQ[$runningLogFile1], Get[$runningLogFile1], Print["Error: Noexist"];]; If[FileExistsQ[$runningLogFile2], Get[$runningLogFile2], Print["Error: Noexist"];];  that I use to read in a list of consecutively named files, m0.txt,m1.txt, m2.txt. I want to make this code more efficient for reading in a large number of such files, for which it would be very tiresome to do in the above way. Can I automate this with a Do or Table? I thought something like  Do["$runningLogFile" <> ToString[i] = "m"<>ToString[i]<>".txt";, {i, 1, 100}];


might work but apparently not.

-
Would Do[With[{file = ToExpression["\$runningLogFile" <> ToString[k]]}, Set @@ {file, ToString[StringForm["m1.txt", k]]}; If[FileExistsQ[file], Get[file], Print["Error: Noexist"]]], {k, 5}] work? – J. M. Jun 20 '13 at 9:46

One way to go about this is read in the file names - this way you don't have to explicitly check to see if they exist.

 path = SystemDialogInput["Directory", NotebookDirectory[]];
fileNames = FileNames["*.txt", path];
allFiles = Table[Import[fileNames[[num]]], {num, 1, Length[fileNames]}];


This reads the names of all files (in the specified path directory) that have the extension ".txt" and places the file names in a list called fileNames. It then reads all the files in this list. They can be be named anything at all, as long as they have the specified extension.

Of course you could use a different extension than .txt, you could specify the directory path in the code (instead of using the GUI), you can use Get instead of Import, and Do instead of Table. You could also filter out files you don't want before importing, if this is desirable.

-
Thanks, this works if you just have files called 0,1,2,3... and change the code as: fileNames = FileNames["*", path]; allFiles = Table[Import[fileNames[[num]]], {num, 1, Length[fileNames]}]; but not otherwise as the files are not just named by numbers. Is there a way to concatonate ".txt" onto num when you do the final read in? – fpghost Jun 20 '13 at 11:27
You misunderstand how it works, I think. This reads the names of all files (in the specified directory) that have the extension ".txt" and places these names in a list called fileNames. Then you read in the files from this list. They can be be named anything at all, as long as they have the specified extension. – bill s Jun 20 '13 at 11:36
Based on the variable names used by the OP, should Import be used instead of Get? It seems to me that unless the logfile has commands to be executed, Get should be avoided in this case. – bobthechemist Jun 20 '13 at 11:44
@bill s Yes, sorry I was being an idiot. I think it did not work the way you had it because of Import vs. Get perhaps. My files contains lots of definitions like f[2]=3.4234324, f[3]=3.35555533,.... etc and using Import for some reason did not set these in the workbook like normal, but when I changed to Get things are defined.. – fpghost Jun 20 '13 at 11:45
@bobthechemist @bill s I have no idea if using Get is dangerous here? It's just that using Import doesn't seem to set the definitions that I mentioned in the my previous comment. – fpghost Jun 20 '13 at 11:47

If you don't want to use a table:

SetDirectory[NotebookDirectory[]];
allfiles = Import[#] & /@ FileNames["*.txt"];

-