My final solution (mybe not the best but it works):
#include <SdFat.h>
const uint8_t chipSelect = 4;
SdFat sd;
void pf(SdBaseFile s, char subdirs[][13], ArduinoOutStream outp, char dirnr = -1) {
SdFile file;
char name[13]; // 8.3 format
while (1) {
if (!file.openNext(&s, O_READ)) break; // no file - end
bool z = file.isDir();
file.getFilename(name); // File/directory name
uint32_t fsize = file.fileSize(); // filesize
file.close(); // closing bevore open the next one
if (z) { // add subdir and run new list
memcpy(subdirs[dirnr + 1], name, strlen(name) + 1);
SdBaseFile n; // new basefile for next directory
uint16_t index = s.curPosition() / 32 - 1; //index of current directory
n.open(&s, index, O_READ); // open the directory
pf(n, subdirs, outp, dirnr + 1); // list files/directorys in this dir
}
else { // print filename
for (byte i = 0; i <= dirnr; i++) // print all subdirectorys
outp << (subdirs[i]) << '\\';
outp << name << ' ' << fsize << "\n";
}
}
}
void printSDroot(ArduinoOutStream cout) {
SdBaseFile s;
char dircount[5][13]; // max 5 subdirs
s.openRoot(sd.vol()); // open root dir
pf(s, dircount, cout);
}
void setup() {
Serial.begin(115200);
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
printSDroot(ArduinoOutStream (Serial)); //Serial,Serial1,Serial2,client .... all who knows print
}
void loop() {}
p.s.: using 2560 and Due