简体   繁体   English

在未知数量的文件中查找最新文件的程序

[英]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.我需要为带有 C 和 FATFS 的 STM32 MCU 编写程序,以在未知数量的文件中找到最新的文件。 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例如: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: function计算两个文件名的时间差:

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);

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.它所做的只是在循环发生之前将第一个文件存储在文件列表中,然后对所有文件进行 go 并将它们的日期与存储的文件进行比较。 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.完成循环后,引用将指向列表中的最新文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM