2
$\begingroup$

Much like this question, I am trying to import data from csv files based on date of creation.

I am running Linux Mint and have a bash script that creates csv files every day with file name of the following format: "MonthFullName-dd-yyyy". When I use the code snippet in the above question's answer as":

dateOrdered = ((names = 
     FileNames["/home/folder_name/*.csv"])[[Ordering[
     FileDate[#, "Modification"] & /@ names

It returns:

"/home/folder_name/February-01-2018.csv", \
"/home/folder_name/February-02-2018.csv", \
"/home/folder_name/February-03-2018.csv", \
"/home/folder_name/February-05-2018.csv", \
"/home/folder_name/February-06-2018.csv", \
"/home/folder_name/February-07-2018.csv", \
"/home/folder_name/February-08-2018.csv", \
"/home/folder_name/February-12-2018.csv", \
"/home/folder_name/February-13-2018.csv", \
"/home/folder_name/February-14-2018.csv", \
"/home/folder_name/February-15-2018.csv", \
"/home/folder_name/January-11-2018.csv", \
"/home/folder_name/January-19-2018.csv", \
"/home/folder_name/January-22-2018.csv", \
"/home/folder_name/January-23-2018.csv", \
"/home/folder_name/January-24-2018.csv", \
"/home/folder_name/January-25-2018.csv", \
"/home/folder_name/January-26-2018.csv", \
"/home/folder_name/January-29-2018.csv", \
"/home/folder_name/January-30-2018.csv", \
"/home/folder_name/January-31-2018.csv"

This is NOT the order in which these files were created. It would seem that further ordering by file name has taken place.

I do not have the option "Creation" instead of "Modification" since I am returned the error: The Creation date of a file is not available on Macintosh and Unix.

How do I order my list by the date of creation without ordering by file name also?

Edit: I just realized that Unix does not have the concept of "creation time". So in my case, what is the best way to sort by files given their obvious file names of "Month-dd-yyyy.csv"?

$\endgroup$
  • 1
    $\begingroup$ It would help to provide the FullForm output of FileDate[#, "Modification"] & /@ names $\endgroup$ – Carl Woll Feb 15 '18 at 23:21
  • $\begingroup$ Btw., with version 11.0.1, I am also able to use FileDate[#, "Creation"] &... (I know that it does not help you, though.) $\endgroup$ – Henrik Schumacher Feb 15 '18 at 23:28
2
$\begingroup$

You may use SortBy in conjunction with DateList to order the list of filenames:

filelist = {
   "/home/folder_name/February-01-2018.csv", 
   "/home/folder_name/February-02-2018.csv", 
   "/home/folder_name/February-03-2018.csv", 
   "/home/folder_name/February-05-2018.csv", 
   "/home/folder_name/February-06-2018.csv", 
   "/home/folder_name/February-07-2018.csv", 
   "/home/folder_name/February-08-2018.csv", 
   "/home/folder_name/February-12-2018.csv", 
   "/home/folder_name/February-13-2018.csv", 
   "/home/folder_name/February-14-2018.csv", 
   "/home/folder_name/February-15-2018.csv", 
   "/home/folder_name/January-11-2018.csv", 
   "/home/folder_name/January-19-2018.csv", 
   "/home/folder_name/January-22-2018.csv", 
   "/home/folder_name/January-23-2018.csv", 
   "/home/folder_name/January-24-2018.csv", 
   "/home/folder_name/January-25-2018.csv", 
   "/home/folder_name/January-26-2018.csv", 
   "/home/folder_name/January-29-2018.csv", 
   "/home/folder_name/January-30-2018.csv", 
   "/home/folder_name/January-31-2018.csv"
};

SortBy[filelist, DateList[FileBaseName[#]] &]

{"/home/folder_name/January-11-2018.csv", "/home/folder_name/January-19-2018.csv", "/home/folder_name/January-22-2018.csv", "/home/folder_name/January-23-2018.csv", "/home/folder_name/January-24-2018.csv", "/home/folder_name/January-25-2018.csv", "/home/folder_name/January-26-2018.csv", "/home/folder_name/January-29-2018.csv", "/home/folder_name/January-30-2018.csv", "/home/folder_name/January-31-2018.csv", "/home/folder_name/February-01-2018.csv", "/home/folder_name/February-02-2018.csv", "/home/folder_name/February-03-2018.csv", "/home/folder_name/February-05-2018.csv", "/home/folder_name/February-06-2018.csv", "/home/folder_name/February-07-2018.csv", "/home/folder_name/February-08-2018.csv", "/home/folder_name/February-12-2018.csv", "/home/folder_name/February-13-2018.csv", "/home/folder_name/February-14-2018.csv", "/home/folder_name/February-15-2018.csv" }

$\endgroup$
  • $\begingroup$ Wow. Very interesting. DateList recognizes the FileBaseName? Or is there something else going on? $\endgroup$ – dearN Feb 15 '18 at 23:24
  • $\begingroup$ Since the FileBaseName is merely a date - it can. Sometimes one simply has to give it a shot =) $\endgroup$ – Henrik Schumacher Feb 15 '18 at 23:27
  • $\begingroup$ Yes, you are right. I didn't really think of it and neither did I know that DateList was a thing (although that should have been obvious, given that FinancialData does use DateListPlot). $\endgroup$ – dearN Feb 15 '18 at 23:35

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.