jamesh
jamesh

Reputation: 20101

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

I have a project written for Android devices. It generates a large number of files, each day. These are all text files and images. The app uses a database to reference these files.

The app is supposed to clear up these files after a little use (perhaps after a few days), but this process may or may not be working. This is not the subject of this question.

Due to a historic accident, the organization of the files are somewhat naive: everything is in the same directory; a .hidden directory which contains a zero byte .nomedia file to prevent the MediaScanner indexing it.

Today, I am seeing an error reported:

java.io.IOException: Cannot create: /sdcard/.hidden/file-4200.html
  at java.io.File.createNewFile(File.java:1263)

Regarding the sdcard, I see it has plenty of storage left, but counting

$ cd /Volumes/NO_NAME/.hidden
$ ls | wc -w
9058

Deleting a number of files seems to have allowed the file creation for today to proceed.

Regrettably, I did not try touching a new file to try and reproduce the error on a commandline; I also deleted several hundred files rather than a handful.

However, my question is:

Nota Bene: The SD card is as-is - i.e. I haven't formatted it, so I would guess it would be a FAT-* format.

The FAT-32 format has hard limits of filesize of 2GB (well above the filesizes I am dealing with) and a limit of number of files in the root directory. I am definitely not writing files in the root directory.

Upvotes: 13

Views: 15597

Answers (4)

flaja
flaja

Reputation: 1

There is a practical limit much lower than 512, depending on the Android device you connect :
When I connect my phone (Samsung A5 2015) to my computer (Ubuntu) : from my computer, I can easily read/write files on my phone, except for a directory which contains 300 files. It is infinitely slow for any simple action : list the content, copy a file, delete a file . . . So, I think that there is a practical limit to the number of files for my phone. I have also tablets with which I do not have this problem.

Upvotes: -1

OferR
OferR

Reputation: 1744

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.

Upvotes: 6

David Webb
David Webb

Reputation: 193714

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.)

Upvotes: 29

Victor
Victor

Reputation: 9269

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

Upvotes: 5

Related Questions