Does anyone know if it is possible to move up a directory using SdFat?
I was hoping to use something like:
sd.chdir("..")
What I am wanting to do is to recurse the sub-directory tree and iterate through all the files. As an example for the following structure, want to work on all files under /subdir_A/, ie files 1, 2 and 3:
@ScruffR, it looks like I will have to revert to reading the sub-dir name at each step (which is a can do and am doing anyhow) and using that to “grow” and “collapse” the path in a string. Not neat, but it will work…
The ls() function is written as a recursive function that will visit all directory entries in the tree below the current directory.
You can implement a function like this by making the current file, ‘this’ in ls(), an argument of the function rather than as a member of the file class.
//------------------------------------------------------------------------------
bool FatFile::ls(print_t* pr, uint8_t flags, uint8_t indent) {
FatFile file;
if (!isDir()) {
DBG_FAIL_MACRO;
goto fail;
}
rewind();
while (file.openNext(this, O_RDONLY)) {
// indent for dir level
if (!file.isHidden() || (flags & LS_A)) {
for (uint8_t i = 0; i < indent; i++) {
pr->write(' ');
}
if (flags & LS_DATE) {
file.printModifyDateTime(pr);
pr->write(' ');
}
if (flags & LS_SIZE) {
file.printFileSize(pr);
pr->write(' ');
}
file.printName(pr);
if (file.isDir()) {
pr->write('/');
}
pr->write('\r');
pr->write('\n');
if ((flags & LS_R) && file.isDir()) {
file.ls(pr, flags, indent + 2);
}
}
file.close();
}
if (getError()) {
DBG_FAIL_MACRO;
goto fail;
}
return true;
fail:
return false;
}
Implementing relative navigation is not easy for FAT. There is a … entry in FAT16/FAT32 but it is a copy of the 8.3 directory so then you must do a search for the actual directory.
exFAT eliminated the … entry so I gave up on relative navigation for SdFat V 2.0 which is now in beta.
The best way is to implement relative navigation is to maintain the entire path in memory as an array of open files. This is expensive in memory and code and my top priority is to support Uno, the classic Arduino.