Using struct=dir(selpath), what do '.' and '..' mean as

19 views (last 30 days)
When I use struct=dir(selpath) and then, I see two extra names ('.' and '..' ) that are not name of folders in the route.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Oct 2019
It is not recommended to skip the first two entries because there are real cases where . and .. will not be the first two entries.
dinfo = dir(selpath);
dinfo([dinfo.isdir]) = []; %get rid of all folders
dinfo = dir(selpath);
dinfo(ismember({}, {'.', '..'})) = []; %get rid of those ones
Amplification: MS Windows and Unix systems do not define a particular sort order for files returned by directory services. The order is defined by the file system driver, and historically has included "write a new file in the first available slot" unsorted. Microsoft's NTFS file system does not appear to have a publicly defined file order, but in practice appears to sort by byte value. Or perhaps it sorts by UTF-16 code point... I do not know.
MacOS's HFS+ filesystem uses 16 bit UNICODE characters "fully decomposed and in canonical order" -- but the canonical order is apparently not the right canonical order, and people who work with file internationalization apparently get pretty frustrated about HFS+ in practice.
Linux does not define a sort order, and I find postings that say that in practice the order can vary between different machines. I see one posting saying that etx4 filesystems end up sorting files according to a hash of their name.
The practical upshot for NTFS (Windows) and HFS+ (Mac) is that file names that begin with any of the characters !"#$%&'()*+,- or space usually sort before '.' in dir() order .

More Answers (0)


Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!