3

I tried to import multiple files at once. Following this topic my code is

files = FileNames["*.phz", NotebookDirectory[]]
neki = Import[#, "Table"] & /@ files

Now this works perfectly fine. To be completely clear my files have names "zug1.phz", "zug2.phz", "zug3.phz" etc... up to "zug25.phz".

Now the problem is that I don't know the order of the imported files. Meaning neki[[1]] opens a file, but the question is, which one? Ideally neki[[1]] would open file "zug1.phz". But it doesn't matter, the idea is that I have to know which file is opened or maybe even specify at the Import which file should be under neki[[1]].

skrat
  • 1,275
  • 1
  • 10
  • 19

1 Answers1

7

The "Details and Options" section of the documentation of FileNames states that "The list of files returned by FileNames is sorted in the order generated by the function Sort." so the list will be in lexicographic order, and I suspect that your files will not be imported as you showed.

files = {"zug1.phz", "zug2.phz", "zug3.phz", "zug4.phz", "zug5.phz", 
   "zug6.phz", "zug7.phz", "zug8.phz", "zug9.phz", "zug10.phz", 
   "zug11.phz", "zug12.phz", "zug13.phz", "zug14.phz", "zug15.phz", 
   "zug16.phz", "zug17.phz", "zug18.phz", "zug19.phz", "zug20.phz", 
   "zug21.phz", "zug22.phz", "zug23.phz", "zug24.phz", "zug25.phz"};

Sort[files]

(* Out: 
{"zug10.phz", "zug11.phz", "zug12.phz", "zug13.phz", "zug14.phz", "zug15.phz", "zug16.phz", 
 "zug17.phz", "zug18.phz", "zug19.phz", "zug1.phz", "zug20.phz", "zug21.phz", "zug22.phz", 
 "zug23.phz", "zug24.phz", "zug25.phz", "zug2.phz", "zug3.phz", "zug4.phz", "zug5.phz", 
 "zug6.phz", "zug7.phz", "zug8.phz", "zug9.phz"}
*)

Patrick Stevens suggested a very nice workaround using an Association in his comment to your question:

<| "Name" -> #, "Contents" -> Import[#, "Table"] |> & /@ files 
MarcoB
  • 67,153
  • 18
  • 91
  • 189