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]].

  • 2
    $\begingroup$ You could use <| "Name" -> #, "Contents" -> Import[#, "Table"] |> & /@ files. I don't know about inbuilt ways to do it. $\endgroup$ Aug 18, 2015 at 14:30
  • 2
    $\begingroup$ Mapping preserves order, meaning Transpose@{files,neki} should create filename/data pairs that aligned. $\endgroup$
    – N.J.Evans
    Aug 18, 2015 at 15:14
  • 2
    $\begingroup$ useful: SortBy[files, StringCases[#, (n : NumberString) ~~ ".phz" :> ToExpression@n] &] $\endgroup$
    – george2079
    Aug 18, 2015 at 20:23

1 Answer 1


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"};


(* 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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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