Get the full path or directory name of a File
SDaron opened this issue · 4 comments
Hello, is there a straightforward way to obtain the file path after initialization? I've been attempting, without success, to implement something similar to entry.getPath(). The entry.getName() function only retrieves the filename, but I require the directory name of an existing File (or the full path for further processing). thanks for your help
There is no way to get the full path. It impossible for exFAT and not reliable for FAT16/Fat32. You need to save the full path when you open the file. exFAT has no entry for the parent directory and FAT only has a pointer to the short filename.
Desktop OS save the full path when a file is opened but this is not practical for MPUs.
I am able to read the file name from the SD since I need the location of the directory entry but not the full path.
Edit: I guess it would be possible if you started at root and did a tree search like ls() until you found the file's directory entry. You could then print the path. This could take forever for a SD with thousands of files.
Thank you for your comprehensive response and suggestions. I'll explore this approach. I assume there must be a specific reason why fatLib doesn't store the path along with the file object upon opening, but creating a wrapper class to keep both the path and the fatFile together could be a practical solution. By the way, it's a fantastic library!
I assume there must be a specific reason why fatLib doesn't store the path along with the file object
Since the spec for a exFAT/FAT32 path is:
Maximum of 32,760 Unicode characters with a maximum of 255 characters per path component.
This could be over 64KB, and an app may want to use a data structure to store path names. I decided not to store the path.
Most users set a working directory with sd.chdir(path)
. This requires no extra memory because the initial cwd is root and the file object is in the SdFat volume class.
If several directory paths are needed, you can use file.open(&dirFile, path, oflag)
. This is easier than manipulating string paths which can be in UTF8 for many languages. About 40-50 bytes is required per dirFile.
In the first version of SdFat I tried to develop a function to open a file's directory entry. FA16/FAT32 directories had entries for
"." (this directory) and ".." (parent directory) .
There were problems. The directory name was not in these entries, just "." or ".." and the start sector for the directory entry containing the name. You still needed to do a search. The move to Long File Names made "." and ".." hard to use.
Then with exFAT these almost useless entries were removed and I gave up trying to open a file's directory.
Even Linux is slow or will fail to return a a files path. It needs to reconstruct the path but this is usually possible since it has sane "," and ".." directories.
Note that the path for an open file can change if a rename is done for some part of the path. rename() can change the directory tree for an open file by moving a files directory to a different parent directory.