How to count how many files exist in the SD card
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-01-22 12:02 PM
Hy guys, I want to be able to create a named file in an SD card based on how many files I have in the directory, per example: if I have only 1 file "file1", I'll create the next one as "file2".
I searched the ff.h library, but didn't find anything that seemed usefull. Has anyone know any ideas I might implement to make it work?
Also alternatively, is there a function that reads or compares the name of the file and then create another file with a different name?
Solved! Go to Solution.
- Labels:
-
FatFS
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-02-02 12:20 PM
IT WORKED,
I added a line to break:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-01-22 12:18 PM
Hi,
Just read the filenames (++ a counter) until end of dir , then you know how many files in this dir .
(I tested : f_readdir needs about 0,5..1ms , so for 30 files need about 30ms .)
Then make a string with "fileX" (X = ++counter) and open file with this new name, thats it.
.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-01-22 12:29 PM
FatFs is designed to be small, there's not specific function to count files, not that I recall one in STDIO or WIN32 either..
Enumerate files via f_opendir() / f_readdir(), filter out directories, and perhaps count FILExxxx.TXT files, or search for largest used case, and increment to the next.
http://elm-chan.org/fsw/ff/doc/readdir.html
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-02-02 9:44 AM
Hi @AScha.3
I've writed the code to do it, it creates a file"0", even tho there already have a file in the SD. But it doesn't create another one, it keeps updating the file0. Here's the code:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-02-02 10:01 AM
Does it count files?
What error does f_open() return?
I cited specific code showing how to count files / directories on a specific path.
It's not hard to corrupt the file system if writing fails or if you lose power, etc.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-02-02 10:12 AM - edited ‎2024-02-02 10:35 AM
Actually no, it isn't entering in the if loop.
The res_dir return FR_INVALID_OBJECT.
I added this:
res = f_opendir(&dir, path);
if(res==FR_OK)
{
res_dir = f_readdir(&dir, &filinfo);
if((res_dir==FR_OK)&&(filinfo.fname[0]!= 0))
{
file_number++;
}
}
And now res is returning FR_INVALID_DRIVE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-02-02 11:00 AM
Solved the issue on the errors by putting: res = f_opendir(&dir, "/");
It created a file1 (it already had file0), but it continues not counting after that, it only updates file1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-02-02 11:35 AM
Right, because you don't loop.
The cited example uses for (;;) it could use a while() variant
It has to iterate though all the files/directories within the object, it has to do this one at a time until all the content is consumed.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-02-02 11:47 AM
Like this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-02-02 12:20 PM
IT WORKED,
I added a line to break:
