Get most recent online file?

Imagine a public directory online, like this one: https://neo.sci.gsfc.nasa.gov/archive/rgb/MOD_LSTD_E/

This directory has a file put into it usually every 8 days. I would like to get the newest file whenever the sketch is run. Now I could simply check for a file added today, and then go backwards a day at a time for 8 days until I find something, but that technique is quite slow.

Does anyone know of a way to examine the creation dates of files in such a directory so that I might be able to find the newest one?

Thank you in advance!

Load the page’s source code which is plain html, extract file names and modification dates and loaded them into a map or some sort of data container so you can sort it.

Kf

1 Like

That’s a good tip, thank you kfrajer.

I’m seeing if I can shortcut that by loading the html into a string, scraping the files names (that’s all I really need) and then only saving the last one it finds, since that is the newest.

OK I think I got it.

This chore is made a little easier by the fact that the files are listed in chronological order.

String[] ln = loadStrings("https://neo.sci.gsfc.nasa.gov/archive/rgb/MOD_LSTD_E/");
String page1 = "";
int ignore; // lines of code to ignore at the top of the webpage

for (int i=0;i<ln.length;i++) {
    //println(i+" "+ln[i]); //print html with line numbers
    page1 = page1+=ln[i];
}

String[][] test = matchAll(page1, ">MOD_LSTD_E_(.*?).PNG</a>");

//print ALL values found
//for (int i = 0; i < ln.length-ignore; i++) {
  //ignore = 12;
  // print(i + " "); println(test[i][1]);
//}

//print only last (most recent) value
ignore = 13;
print(test[int(ln.length-ignore)][1]);
1 Like