4

I have a folder that contains certain files I want to import. In my case those are .png files. As all the files I want to import contain the phrase "accum" in their filenames, I can use

SetDirectory[directory]; (*directory is string of the directory that contains the files*)
importString = "./*" <> "accum" <> "*";
files = Import[importString];

to obtain a list consisting of all the images I wanted to import. Of course this list is sorted by filename. What can I do to tell Import that the files should not be sorted by "name" but by other options like "date added" (which is what I want)?

Wizard
  • 2,720
  • 17
  • 28

2 Answers2

10

Likely something like the following:

Import[#, "PNG"] & /@ SortBy[FileNames[importString], FileDate[#,"Creation"]&]

although I suspect there might be a more terse approach.

chuy
  • 11,205
  • 28
  • 48
2

You can use something like this. You may need to adapt it.

ls = Import["!ls -al 2>&1", "Text"];
lines = Rest@StringSplit[ls, "\n"];
parts = StringSplit /@ lines;
dates = parts[[All, 6 ;; 8]];
names = parts[[All, 9]];
datedfiles = DeleteCases[MapThread[Prepend, {dates, names}], {"." | "..", _}];
Column[datedfiles]

{"ans.png", "Jan", "22", "23:53"}

{"curve1.png", "Jan", "5", "00:44"}

{"data.sav", "Jun", "20", "2013"}

{"test.mx", "May", "1", "2013"}

This list can then be processed and the files imported as required.

Additional Note

Timings to find the name of the most recent file in a large directory of files.

This is pretty much instant :

StringDrop[Part[Rest@
   StringSplit[Import["!dir /od /a:-d 2>&1", "Text"], "\n"], -3], 36]

But this is very slow :

Last@SortBy[FileNames[], FileDate[#, "Creation"] &]
Chris Degnen
  • 30,927
  • 2
  • 54
  • 108