Unable to open more than one file on 4.1 using SD.h

MartinJR

Member
I am unable to open more than one file at a time on the 4.1 using the builtin SD card and SD.h and haven't found any clues in the example code provided. This means things like recursion through file handles to list files on the card, etc. isn't possible.

Code like below fails because tmpFile2 can't be opened:

Code:
File tmpFile1, tmpFile2;

tmpFile1 = SD.open("/");
while ( tmpFile2 = tmpFile1.openNextFile() ) {

   // ...

   tmpFile2.close();
}

Any suggestions please? Thanks.
 
This means things like recursion through file handles to list files on the card, etc. isn't possible.

In Arduino, click File > Examples > SD > listfiles.

The listfiles example does exact this, open the root folder, then recursively find every file by openNextFile().

If you still need more help, show us a complete program we can copy into Arduino and run on a Teensy 4.1 with a SD card to reproduce the problem. Often small details which initially seem unimportant can end up making all the difference. To really help you, we need to see the complete program.
 
In Arduino, click File > Examples > SD > listfiles.

The listfiles example does exact this, open the root folder, then recursively find every file by openNextFile().

If you still need more help, show us a complete program we can copy into Arduino and run on a Teensy 4.1 with a SD card to reproduce the problem. Often small details which initially seem unimportant can end up making all the difference. To really help you, we need to see the complete program.


Unfortunately I can't post the whole code - it's part of a few thousand lines which is partly why I haven't yet homed in on the problem! However, the part in question simply deletes files older than a certain age, hence my puzzlement at not being able to open the second file.

If something silly were to happen, like calling 'remove' on a file before 'close', could that be enough to upset the SD library to prevent subsequent operation?

Thanks
 
Without seeing 'problem code', to recreate here (or there) seems starting with the p#2 listfiles.ino and editing to match that code to perhaps delete every other file during the list process. Try it with the file closed and without as that may point out the trouble with remove.

If that example can be written to fail it could be posted.
 
Back
Top