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


2 Answers 2


Likely something like the following:

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

although I suspect there might be a more terse approach.


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}], {"." | "..", _}];

{"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 :

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

But this is very slow :

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

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.