Is there a limit for the number of files in a directory on an SD card?

There's a limit on 512 entries in the root directory of FAT filesystem. This limit comes about because the root directory lives in a particular place on FAT filesystems.

For other directories this limit is not in place. Additionally, FAT32 removed the 512 entry limit for the root filesystem by treating the root directory the same as any other directory.

Using long filenames - i.e. not in 8.3 format - means than a single file uses multiple directory entries.

Some Googling finds some people claiming that a FAT32 directory can have a maximum of 65,536 entries (which would be fewer files if they had long file names). However, none of the sources that mentioned this limit seemed that reliable so I thought I'd test this.

I wrote a script which creates files with 30 character filenames, meaning each file would need 4 directory entries. When the script got to file 16,384 it failed with an IO error and I couldn't create more files in my test directoy. So this does seem to validate the 65,536 entry limit.

If you're hitting this limit at 9,000 files then your files must be using at least 7 entries each which corresponds to filenames that are at least 66 characters long. Does this match what you're doing? (Or you could have some short filenames and some very, very long ones, but you get the idea.)


I think the limit of files in a directory in Fat32 also depends on the length of the filenames

http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx


I have done some more testing ( reliable testing :-) ), trying to write 100,000 short-named directories in a single directory. The limit was reached at 65,536.

The test was done on Nexus One running Android 2.2, but I believe the result holds for any FAT32 SD card.