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?
Asked
Active
Viewed 229 times
1 Answers
-1
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();
}