counting files in SD

Hello all,

please help me with this, I searched through the forum but I haven't found the same silly question elsewhere, (if I missed it, I apologize).

Here is my supersimple routine to count files using openNext() in a directory in my micro sd card:

unsigned int numMP3files = 0;

while (file.openNext(sd.vwd(), O_READ)) {


This gives the wrong number of files (the double) and also with strange names when I list the content of the directory


there is one file named "12345678.mp3"

the console says there are two files, one named correctly and another named like "__1234~1.mp3"

I am a total beginner but the code is super simple and I cannot understand why it behaves this way

I checked for invisible files but there are no.

It might be important to say that I am working on a Bare conductive touch board.

Thanx for your time and for your help


if your file has a space in the name it will show an incorrect file name.

Hi John,

thanx, but I only used 8 characters long names with no spaces, just like ABCDEFGH.mp3

Since nobody else suggested a solution to such a simple problem, then I guess it's simply something that shouldn't happen. Or maybe it depends on the bare conductive board.

Will keep on trying.



How do you create your files on the SD? Do you use a PC with Windows, a Mac, or Linux?

Names like ABCDEFGH.mp3 are not considered long file names by Windows. The rule is that 8.3 names that have all upper or all lower case in the base name and all upper or all lower in the extension are short names.

These are short 8.3 names: BASE.EXT, BASE.ext, base.EXT, base.ext. Windows uses two bits in the short directory entry to indicate the character case of the base and extension.

Some programs create an incorrect extra entry. Try using Windows to check the SD for file system errors.

Try formatting the SD with SD Formatter on Windows and recreate the files on Windows.

Thank you fat16,

actually I am on a mac and I simply put files in the micro SD via a USB adapter. I will try on a windows machine and see if it changes.

And thanx for the explanation on short names. Will come up with some more tries now.



ok, so the problem must be in the process of copying the files into the sd card. It doubles every file I copy in it and changes its name with ~ and numbers.

It does it anyway, also if I put a file named P.MP3 then I will have two files in the directory, one named correctly P.MP3 and another named _~1.MP3

I tried with changing the length of the file names up to 8 characters but nothing changed.

like TRACK001.MP3 becomes _TRACK~1.MP3

it looks like the names are too long or contains empty spaces but it's not (at least on the mac view). Will try to copy files from a windows machine.


The Mac creates two file, the standard FAT file and the resource fork information written as a separate file.

The second file should be "hidden" but openNext opens all files.

You can use something like this:

    // Skip directories and hidden files.
    if (!file.isSubDir() && !file.isHidden()) {

See this example. It skips directories and hidden files.

oooh.. this saved me. Thanx a lot !


Admant, could you share the entire code?

I dont know how to apply what you shared in the original post


Hello camilozk, sorry for long absence, do you still need the code?

hello fat16lib,

your code up here doesn't seem to work anymore. The isHidden() property returns an error. Is a way to get this boolean some other way? I posted also in a separate topic, but will copy the solution if I find it. it is very useful for me.



Hi Antonio!

I would be pleased if you could share the code, yes.

thank you!

Hey mates,

I exactly have the same problem right now and it causes me a lot of trouble.

Have you found any solutions to replace this useful isHidden() function ?

I can't find anything similar...

Thx a lot !

I had the same problem. The one thing I noticed about the duplicate files is that they all had a ~. So I just ignored files that had a ~ in them. It is kinda a hack, but it works. Just don't legitimately put a ~ as part of your file names.

    String file_name =;

    if( file_name.indexOf('~') != 0 )
       //This is an actual file.

I was trying to count the files in a directory and I would always have double files until I started ignoring all files with ~.