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.

  • How to attach javadoc or sources to jars in libs folder?
  • Android: java.lang.IllegalArgumentException: Invalid payload item type
  • How to remove underline below EditText indicator?
  • How to retrieve an Facebook-AuthToken from the accounts saved on Android
  • How do I add a horizontal RecyclerView inside a list item
  • How to convert a LatLng and a radius to a LatLngBounds in Android Google Maps API v2?
  • 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:

    • are there hard limits on filesize or number of files in a directory?
    • am I even on the right track here?

    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.

    Related posts:

    FileNotFoundException for HttpURLConnection in Ice Cream Sandwich
    Update tabs from a tab's activity in a Android TabHost
    No shortcut for DDMS in Android Studio 2.2
    How can I get a file from CamScanner or Office Lens in a web app running on Android?
    Android merge two images
    Is there an easier way to add gson to my project?
  • How to implement my very own URI scheme on Android
  • Android: invalidate(dirty)
  • Java MTP library that works on Windows
  • Android Click on listItem checks wrong checkbox
  • Best practice for tab navigation?
  • Fragment onResume() & onPause() is not called on backstack
  • 3 Solutions collect form web for “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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.