Abhi
Abhi

Reputation: 303

Find the file having lowest timestamp

I have a requirement which needs to be done by only using Java Script and would appreciate if someone can help here.

  1. We have 10 files coming into a folder of format MMddyyyyHHmmss (ex. 07192013114030) - MonthDayYearHourMinuteSecond
  2. The file gets dropped from an external system once every day
  3. When the 11th file comes in I need to find the file that was dropped on the first and delete it so that the total count of the files should always be 10 (latest 10 files)

Sample example

07192013114030
07202013114030
07212013114030
07222013114030
07232013114030
07242013114030
07252013114030
07262013114030
07272013114030
07282013114030

When the 11th file comes in on 07292013114030, I want to find the file 07192013114030 using Java Script.

I can provide the incoming file names in any format, ex. MM/dd/yyyy/HHmmss or MM_dd_yyyy_HH_mm_ss if that helps to do this using JS

Upvotes: 1

Views: 199

Answers (3)

Travis
Travis

Reputation: 12379

Since you can get the dates in any format, get them in YYYYMMDDHHmmss format. Then get those timestamps in an array. There's not enough information about your system in your question to explain how to do this but just loop through the files pulling out the timestamps and pushing them into an array.

Basically you should have an array like this when you're done:

dates = ['20130719114030',
         '20130720114030',
         '20130721114030',
         '20130722114030',
         '20130723114030',
         '20130724114030',
         '20130725114030',
         '20130726114030',
         '20130727114030',
         '20130728114030'];

Once done, simply sort the array:

dates.sort();

Dates will be in alphanumeric order, which also happens to be chronological order because of our date format. The oldest date will be the first one in the array, so

dates[0] // '20130719114030'

Again, there's not enough information about your system to explain how to delete the file, but perhaps you could loop through the files again to find a matching timestamp, then delete the file.

Upvotes: 1

akash.trivedi
akash.trivedi

Reputation: 81

Convert them all to date objects and then compare them. You would only have to do two pass throughs of the list to find the smallest date (one to convert and one to compare)... instead of extracting each snippet and going through the list multiple times.

http://www.w3schools.com/js/js_obj_date.asp

Upvotes: 0

oas8df79as8fysf
oas8df79as8fysf

Reputation: 41

I'm not experienced with Javascript, but my logical progression would be:

Out of the 11 files, find the lowest year If the same Out of the 11 files, find the lowest month [...] all the way down to second

Upvotes: 0

Related Questions