# Import multiple files from folder in order of date added

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)?

-

Likely something like the following:

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


although I suspect there might be a more terse approach.

-
+1 - very neat. –  Chris Degnen Apr 18 '14 at 19:04

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.

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"] &]

-
Caveat: will not work on your average Windows machine. –  Yves Klett Apr 18 '14 at 20:22
Yes, use Import["!dir 2>&1", "Text"] on Windows, and extract parts accordingly. –  Chris Degnen Apr 18 '14 at 20:24
Good method (+1) but consider using: ReadList["!dir /od /a:-d 2>&1", String] -- see e.g. (2401) –  Mr.Wizard Jul 16 '14 at 10:07
@Mr.Wizard - ah yes, Last@ReadList["!dir /od /a-d /b", String] –  Chris Degnen Jul 16 '14 at 11:24