8
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Would Do[With[{file = ToExpression["$runningLogFile" <> ToString[k]]}, Set @@ {file, ToString[StringForm["m`1`.txt", k]]}; If[FileExistsQ[file], Get[file], Print["Error: Noexist"]]], {k, 5}] work? $\endgroup$ Commented Jun 20, 2013 at 9:46

2 Answers 2

8
$\begingroup$

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.

$\endgroup$
11
  • $\begingroup$ 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? $\endgroup$
    – fpghost
    Commented Jun 20, 2013 at 11:27
  • $\begingroup$ 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. $\endgroup$
    – bill s
    Commented Jun 20, 2013 at 11:36
  • $\begingroup$ 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. $\endgroup$ Commented Jun 20, 2013 at 11:44
  • $\begingroup$ @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.. $\endgroup$
    – fpghost
    Commented Jun 20, 2013 at 11:45
  • $\begingroup$ @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. $\endgroup$
    – fpghost
    Commented Jun 20, 2013 at 11:47
1
$\begingroup$

If you don't want to use a table:

SetDirectory[NotebookDirectory[]];
allfiles = Import[#] & /@ FileNames["*.txt"];
$\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.