I have a File
variable called dir
and i'm building a simple file explorer for my device. Is there a way to get a reference to dir
's parent directory?
-
Doesn't dir.name() return dir's name with the path?– ocrduCommented Dec 14, 2020 at 19:18
-
Unfortunately it only returns the name– none noneCommented Dec 14, 2020 at 19:43
-
1Then probably the only way is to keep a list of the directories you traversed while going to that file, or while building the directory structure from root. There may be an other way, but I don't know the SD library well enough.– ocrduCommented Dec 14, 2020 at 20:03
Add a comment
|
1 Answer
I don't think there is an inbuilt function returning the parent, but you could do:
FS &file_system = SD; // pick whichever file system you use
File get_parent(File dir) {
String dirPath = dir.path();
int lastSlashIndex = dirPath.lastIndexOf('/');
if (lastSlashIndex != -1) {
String parentDirPath = dirPath.substring(0, lastSlashIndex);
File parentDir = file_system.open(parentDirPath.c_str());
return parentDir;
}
return File();
}
-
1but there is no
path
function in the Arduino SD library. (and no FS type)– Juraj ♦Commented Aug 28, 2023 at 13:11 -
I only have esp32-S3 at hand here, and path() is available in framework-arduinoespressif32 -> FS.h, I don't know the situation for other platforms/architectures. Commented Aug 29, 2023 at 16:25