FileNames retrieves a list of all files in a directory. But is there a way to order these files by their most recent modification time?

  • 5
    $\begingroup$ Try this: SortBy[FileNames["*", {Directory[]}], FileDate]. $\endgroup$ Mar 27 '20 at 22:00
  • $\begingroup$ @LeonidShifrin that would make a good answer (it works). $\endgroup$
    – C. E.
    Mar 27 '20 at 22:02
  • $\begingroup$ @C.E. Done as requested. $\endgroup$ Mar 29 '20 at 14:43

You need FileDate. It returns a DateObject, and is therefore directly suitable to be used in SortBy. For example:

 SortBy[FileNames["*", {Directory[]}], FileDate]

FileDate takes an optional second argument, which can specify types of changes one is interested in: "Access", "Change", "Creation", and "Modification", with the latter being the default (see documentation for more details).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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