Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 8 down vote accepted

Likely something like the following:

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

although I suspect there might be a more terse approach.

share|improve this answer
+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}], {"." | "..", _}];

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

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

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

{"", "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"] &]
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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