Pages: [1]   Go Down
Author Topic: Sdfat  (Read 7564 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

Does anyone know how to retrieve the file creation and modification date with Sdfat? I know it this information is returned with root.ls(..) command, but that returns all the files in the directory. I just want this for a specific file.

Thanks,

-ren
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 7
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using SdFat library:

Code:
#include <SdFat.h>
#include <SdFatUtil.h>

Sd2Card  card;
SdVolume volume;
SdFile   root;
SdFile   file;


const int SDChipSelect = 9; // For my setup.

void setup(void)
{
    Serial.begin(9600);
    pinMode(SS_PIN, OUTPUT);    // Actually the Sd2Card constructor does this
    digitalWrite(SS_PIN, HIGH); // Make sure Ethernet SPI is not selected

    Serial.println("Type any character to start");
    
    while (!Serial.available())
        ;
        
    Serial.println();

    //
    // If SPI_FULL_SPEED doesn't work with your setup, try SPI_HALF_SPEED
    //
    if (!card.init(SPI_FULL_SPEED, SDChipSelect)) {
        Serial.println("card.init failed");
        return;
    }

    // initialize a FAT volume
    if (!volume.init(&card)) {
       Serial.println("volume.init failed");
       return;
    }
    Serial.print("Volume is FAT");
    Serial.println(volume.fatType(),DEC);
    Serial.println();


    // open the root directory
    if (!root.openRoot(&volume))
    {
        Serial.println("openRoot failed");
        return;
    }

    // open a file
    if (file.open(&root, "TESTFILE.TXT", O_READ)) {
        Serial.println("Opened TESTFILE.TXT");
    }
    else {
        Serial.println("file.open failed");
        return;
    }
    Serial.println();
    
    dir_t d;
    if (!file.dirEntry(&d)) {
        Serial.println("file.dirEntry failed");
        return;
    }
  

    Serial.print("Created      : ");
    file.printFatDate(d.creationDate);
    Serial.print(' ');
    file.printFatTime(d.creationTime);
    Serial.println();

    Serial.print("Last Modified: ");
    file.printFatDate(d.lastWriteDate);
    Serial.print(' ');
    file.printFatTime(d.lastWriteTime);
    Serial.println();  

    Serial.print("Last Accessed: ");
    file.printFatDate(d.lastAccessDate);
    Serial.println();  
}

void loop(void) {}


Change the SDChipSelect definition to whatever your SD circuit uses

Note that if your SD circuit is on a separate shield and you don't have a new Ethernet shield plugged into your Arduino, you don't need the pinMode and digitalWrite to SS_PIN, but I put them there "just in case."  

Output for that file on my 8 Megabyte SD card:

Volume is FAT16

Opened TESTFILE.TXT

Created      : 2011-01-09 12:22:12
Last Modified: 2011-01-09 12:22:12
Last Accessed: 2011-01-21



Regards,

Dave

Footnote:
If you have a larger SD card or microSD card formatted as FAT32 there is really, really (really) no need to reformat it when using the SdFat library.  Really.  On the other hand if you have a really small card that came as FAT12, then you need to reformat it to FAT16.
« Last Edit: January 21, 2011, 08:08:28 pm by davekw7x » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dave,

Thanks for your reply. I must have a different version of sdfat. I removed the & in front of the variables and the code worked great.

Thanks,

-ren
Logged

Pages: [1]   Go Up
Jump to: