Go Down

Topic: delete sd card files every year (Read 307 times) previous topic - next topic

blastboot

hello, i've been thinking what is the best way to delete files every year.
i use a RTC to get date and time and save it to a log file with some variables.

Code: [Select]
Log = file.open(&root, LogFile, O_WRITE | O_APPEND);

  if (Log) {
    Date_Time();
   
    file.print(dayMonth, DEC);
    file.print(" ");
    file.print(Month);
    file.print(" ");
    file.print("20");
    if (year < 10) {
      file.print("0");
    }
    file.print(year, DEC);
    file.print(" , ");
    if (hour < 10) {
      file.print("0");
    }
    file.print(hour, DEC);
    file.print(":");
    if (minute < 10) {
      file.print("0");
    }
    file.print(minute, DEC);
    file.print(":");
    if (second < 10) {
      file.print("0");
    }
    file.print(seconf, DEC);   
    file.print(" , ");
    file.print(data1, 1);
    file.print(" , ");
    file.println(data2);
    file.close();
    Log = false;
  }
  else {
    Serial.println(F("ERROR"));

thanks in advance

PeterH

Do you mean you want to delete files as they reach one year old? I seem to remember that the SDFat library supports file times, so I think it would be possible to detect files that were over a year old. If that wasn't possible, you could adopt a file naming scheme that incorporated the date so that you can determine the age of each file. When you create a new file would be a good time to check for obsolete files.
I only provide help via the forum - please do not contact me for private consultancy.

blastboot

#2
May 26, 2013, 01:09 am Last Edit: May 26, 2013, 01:18 am by blastboot Reason: 1
Can you give me an example or something to read about it? i thought about compare the date and time to see if it is: 31-12   and 23:59 the program dele the files, but it will cause the program to everytime it logs a value to compare the date and time and i thought that maybe could be some better options

PeterH

I'm assuming here that you're trying to provide something analogous to rotating log files. In other words, you have a 'current' log file which new entries get appended to as they arrive. Once the criteria for starting a new log file are met, the old file is closed and a new one is opened. The criteria could be based on the size of the file, or the elapsed time that the file has been open for, or time-of-day passing some boundary such as midnight.

Note that the concept of 'starting a new log file' at some point does not imply that the 'current' file is actually held open for the whole time that it remains current. You could easily open, write and close the file each time an entry needs to be added, and that might be sensible if the rate of new entries was low since it would avoid the possibility of file corruption due to the Arduino being switched off while the file was open. Or, keep the file open for as long as it remains current if throughput is more important to you.

Supposing that you want to change the file at midnight, what you need to do is work out what the current day is i.e. the number of whole 24-hour periods since the epoch. It's easy enough to work out but the implementation depends on the API that you use to access the RTC and I don't know what you're using.

If the cost of working out what the current day is for each log entry seems excessive then you can add another layer of logic keeps a track of the millis() value when the current file become current, and only check whether it's time to change to a new file if 24 hours (or whatever period you use) has elapsed since then. The code for this would be very similar to the 'blink without delay' example, but instead of turning LEDs on and off you would be deciding what log file to use, closing off the old one, choosing a name for the new one, and creating it.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up