stm32fatfs

Program to find the newest file among unknown number of files


I need to write a program for a STM32 MCU with C and FATFS, to find the newest file among unknown number of files. Files names contain their creation dates, the numbers in the file name are separated with "_". for example: oil_sensor_22_07_20_13_15.csv

I have written a code to extract date and time from the files and to calculate their time difference. But I don't know how to find the newest file among all the files.

I include the code which calculates the time difference between two files and the code which finds the newest file among two files.

The function to calculate the time difference between two file names:

double calc_passed_secs(char * name_str_01, char * name_str_02 ){

struct tm tm_struct_1,tm_struct_2;
time_t time_t_1,time_t_2;


// Returns first token
char* token = strtok(name_str_01, "_");

token = strtok(NULL, "_");

token = strtok(NULL, "_");
tm_struct_1.tm_mday = atoi(token);

token = strtok(NULL, "_");
tm_struct_1.tm_mon = atoi(token);

token = strtok(NULL, "_");
tm_struct_1.tm_year = atoi(token);

token = strtok(NULL, "_");
tm_struct_1.tm_hour = atoi(token);

token = strtok(NULL, "_");
token = strtok(token, ".");
tm_struct_1.tm_min = atoi(token);

tm_struct_1.tm_sec = 0;

// Returns first token
token = strtok(name_str_02, "_");

token = strtok(NULL, "_");

token = strtok(NULL, "_");
tm_struct_2.tm_mday = atoi(token);

token = strtok(NULL, "_");
tm_struct_2.tm_mon = atoi(token);

token = strtok(NULL, "_");
tm_struct_2.tm_year = atoi(token);

token = strtok(NULL, "_");
tm_struct_2.tm_hour = atoi(token);

token = strtok(NULL, "_");
token = strtok(token, ".");
tm_struct_2.tm_min = atoi(token);

tm_struct_2.tm_sec = 0;

double seconds = difftime(mktime(&tm_struct_1),mktime(&tm_struct_2));

printf("\r\nTime difference in seconds: %.f\r\n",seconds);

return seconds;

}

and the program to find the newest file (the program does not give the desired results).

  char newest_log_file[128];
  char oldest_log_file[128];


  char first_log_file [128];
  char second_log_file [128];

  char first_log_file_cpy [128];
  char second_log_file_cpy [128];


  //char printf_buff [128];

  // find first file
  fr = f_findfirst(&dj, &fno1, "", "oil_sensor_*.csv");

  strcpy( first_log_file,fno1.fname);

  strcpy(first_log_file_cpy, first_log_file);

  if (!fno1.fname[0]) {
      bool make_first_log_file = true;
  }

  if (fno1.fname[0]) {
      fr = f_findnext(&dj, &fno1);
  }

  if (fno1.fname[0]) {
      strcpy(second_log_file,fno1.fname);

      strcpy(second_log_file_cpy, second_log_file);
  }


  printf("\r\nFirst Log File: %s\r\n", first_log_file);

  printf("\r\nSecond Log File: %s\r\n", second_log_file);


  double seconds = calc_passed_secs(first_log_file , second_log_file);

  if (seconds < 0){
    strcpy(newest_log_file, second_log_file_cpy);
  }

  if (seconds > 0) {
    strcpy(newest_log_file, first_log_file_cpy);
  }

  printf("\r\nnewest file: %s\r\n", newest_log_file);

  do {

      f_findnext(&dj,&fno1);

      strcpy(first_log_file, fno1.fname);

      strcpy(first_log_file_cpy, first_log_file);

      f_findnext(&dj,&fno1);

      strcpy(second_log_file,fno1.fname);

      strcpy(second_log_file_cpy, second_log_file);

      printf("\r\nFirst Log File: %s\r\n", first_log_file);

      printf("\r\nSecond Log File: %s\r\n", second_log_file);

      if (seconds < 0){
        strcpy(newest_log_file, second_log_file_cpy);
      }

      if (seconds > 0) {
        strcpy(newest_log_file, first_log_file_cpy);
      }

      printf("\r\nnewest file: %s\r\n", newest_log_file);


      seconds = calc_passed_secs(first_log_file, second_log_file);

} while (fr == FR_OK && fno1.fname[0]);

  f_closedir(&dj);

Solution

  • The algorithm is pretty simple, pseudocode below:

    newest_file = first_file
    for file in file_list:
      if file.date > newest_file.date:
        newest_file = file
    

    All it does is that it stores first file on file list before loop happens, then you go over all files and compare their date with the stored one. If it's newer, than you change the stored file to the one that you've just checked, then continue looping. Once you're done looping, the reference will point to the newest file on list.