SD Card

28 Feb 2011

Hi all,

Actually, a way exist to count the number of files present in a directory ?

Regards,

01 Mar 2011

Here's what I've used:

#include "mbed.h"
#include "SDFileSystem.h"

int listFiles()
{
    // Your pin numbers will vary....
    SDFileSystem sdcard( p5, p6, p7, p8, "sd" );
    DIR * sdDir = opendir("/sd");

    struct dirent *p;
    int numFiles = 0;
    
    // Read in patterns off of the SD card
    while ((p = readdir( sdDir )) != NULL)
        numFiles++;

    return numFiles;
}

The snippet above counts the files at the top level. If you want to count files in a specific directory, you'd call opendir on that rather than the root of the device.