Implementing recursive "dir" command: I can't call "dir" 3 times :-)

I’m writing a small interpreter for simple SD commands like “dir” or “type the content of a specific file in serial monitor” or others.
The “dir” command has been implemented recursively using printDirectory(), like in the examples.
The strange situation is the following: if I complie the code and call printDirectory() one time, it works.
If I compile “dir” and call two times in sequence, it do wors.
But if I compile and then call three times, the third call doesn’t find any file; it seems that a sort of “close” or “rewind” of the SD have to be made, but I wasn’t able to find what to do.
Here the failing code with the 3 call in sequence:


#include <SD.h>

File root;

void setup()
{
Serial.begin(9600);
SD.begin(4);

Serial.println("****************************************");
SD.begin(4);
root = SD.open("/");
printDirectory(root, 0);
Serial.println(“done!”);

Serial.println("****************************************");
SD.begin(4);
root = SD.open("/");
printDirectory(root, 0);
Serial.println(“done!”);

Serial.println("****************************************");
SD.begin(4);
root = SD.open("/");
printDirectory(root, 0);
Serial.println(“done!”);

}

void loop()
{
// nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs) {
Serial.println(“Starting directory analysis…”);
while(true) {

File entry = dir.openNextFile();
if (! entry) {
// no more files
Serial.println(“nomorefiles”);
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print(’\t’);
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
}
}

You should only call SD.begin(4); once.

Pete

Thanks Pete,
I've tried but the output of the third "call"remain the same:

Starting directory analysis..
SYSTEM~1/
Starting directory analysis..
INDEXE~1 76
nomorefiles
EXAMPLE2.TXT 0
EXAMPLE3.TXT 0
TEST.TXT 126
PIPPO 18
PLUTO 18
PAPERINO 18
MINNIE 18
CLARAB 18
QUI 18
nomorefiles
done!


Starting directory analysis..
SYSTEM~1/
Starting directory analysis..
INDEXE~1 76
nomorefiles
EXAMPLE2.TXT 0
EXAMPLE3.TXT 0
TEST.TXT 126
PIPPO 18
PLUTO 18
PAPERINO 18
MINNIE 18
CLARAB 18
QUI 18
nomorefiles
done!


Starting directory analysis..
nomorefiles
done!

That isn’t the complete code for printdirectory.
You need entry.close():

    } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
     entry.close();

Note my use of code tags. Read How to post code properly and then use the </> icon to create

[code]...[/code]

tags around your code so that it is easier to read.

Pete

Thanks a lot, Pete.
Unfortunatly, despite your help, the problem wasn't solved.

But, after reading one more time the language gude, I have inserted a "caller function" (that should be called from the main) named "dir()" and this function dir() calls printdirectory().
Finally the problem was solved inserting a call to root.rewindDirectory(); just before the first call of printdirectory().

At the and, this is the code of the dir() function:

void dir()  {
   root = SD.open("/");
   root.rewindDirectory();
    printDirectory(root, 0);
   root.close();
}

Being the SD seen from a program as an abstract data, I suppose that the call to root.rewind() resets some global status variable defined and used inside the SD library.

At the end, we got it!! :slight_smile:

Thanks a lot

Fabio