Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
add comment

2 Answers 2

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 at 19:04
add comment

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"] &]
share|improve this answer
    
Caveat: will not work on your average Windows machine. –  Yves Klett Apr 18 at 20:22
    
Yes, use Import["!dir 2>&1", "Text"] on Windows, and extract parts accordingly. –  Chris Degnen Apr 18 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 at 10:07
    
@Mr.Wizard - ah yes, Last@ReadList["!dir /od /a-d /b", String] –  Chris Degnen Jul 16 at 11:24
add comment

Your Answer

 
discard

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.