I am on a vxworks 6.9 platform. I want to know how many files are in a folder. The file system is DOSFS (FAT). The only way I know how to do this is to simply loop through every file in the folder and count. This gets very expensive the more files in the folder. Is there a more sensible way to do this? Does there exist some internal database or count of all files in a folder?
Asked
Active
Viewed 238 times
1
-
Reading a directory should be O(n) just like reading a file. If you're getting worse behaviour than this, that is the time to read each directory entry gets slower as you read through the directory then there might be something wrong with your code (or maybe the VxWorks FAT code). – Ross Ridge Feb 15 '19 at 18:34
-
@RossRidge It's also accessing an SD card so it's pretty slow – peer_2_peer_2 Feb 15 '19 at 19:21
1 Answers
2
The FAT filesystem does not keep track of the number of files it contains. What it does contain is:
- A boot sector
- A filesystem information sector (on FAT32) including:
- Last number of known free clusters
- Number of the most recently allocated cluster
- Two copies of the file allocation table
- An area for the root directory (on FAT12 and FAT16)
- Data clusters
You'll need to walk the directory tree to get a count.

dbush
- 205,898
- 23
- 218
- 273
-
Thanks. So none of the above features can be used as a quicker way to figure out how many files are in a folder? – peer_2_peer_2 Feb 15 '19 at 16:40
-
1@peer_2_peer_2 Correct, the above structures only tell you about clusters, not files. – dbush Feb 15 '19 at 16:41