0

To be able to set the date and time of the files on my SD card I followed this answer: https://arduino.stackexchange.com/a/39127/49678

Now I am wondering How do I read the values out in the microcontroller? To clearify, I get the correct timestamps when I remove the SD card and view them on a PC, but I also want to be able to read this on the microcontroller.

Inside SdFile.cpp on line 1025 there is a function called timestamp which is used to set the timestamp but I cant seem to find any function to read it. https://github.com/adafruit/SD/blob/master/utility/SdFile.cpp

I would prefer a solution where I don't have to add it to the library myself.

The underlying problem I am facing is that I want to log files, and once the memory is full I which to remove the oldest files.

1

In the FAT filesystem the timestamps and other attributes are stored in the directory, not in the file. So you need to read the "file" that is the directory that contains the file you're interested in.

That "file" contains entries in a pre-defined structure. Reading each block of data using that structure gives you the information for each file in turn.

You can just do a normal SD.open(...) call to open the directory that contains your file, and then read chunks of data from it to get each file entry.

That data is arranged in the directoryEntry structure (here), so reading a block of data with File.read(...) into a variable of that structure type will yield your data:

struct directoryEntry dirent;

// myDir has already been opened and points at the start of your dir
while (myDir.read((uint8_t *)&dirent, sizeof(struct directoryEntry))) {

    // Names are tricky to print. Also you should check for deleted files here.
    Serial.print("Name: ");
    for (int i = 0; i < 11; i++) {
        Serial.print(dirent.name[i]);
        if (i == 7) {
            Serial.print(".");
        }
    }
    Serial.println();
    Serial.print("Creation Time: ");
    Serial.println(dirent.creationTime);
    Serial.print("Creation Date: ");
    Serial.println(dirent.creationDate);
}
2

You need to get an SdFile object for your file then use the SdFile::dirEntry method to get the directory entry for the file.

The directory entry (struct directoryEntry) has the timestamps.

  • Please tell that this is for the SdFat library. The example for dirEntry is the same I gave: github.com/greiman/SdFat/blob/master/examples/Timestamp/… – Jot Sep 20 '18 at 16:19
  • @Jot, the SD library is the SdFat library wrapped into simplification with Stream implementation. it should be possible to use the SdFat part to get to timestamp – Juraj Sep 20 '18 at 18:27
  • Thank you for your answer, however I am unsure how to do this. How would I go about to get an SdFile object? The only function I can find that returns SdFile in SDClass is: SdFile SDClass::getParentDir(const char *filepath, int *index) – Heneer Sep 21 '18 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.