MATLAB Answers

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

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

  0 Comments

Sign in to comment.

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.
Instead:
dinfo = dir(selpath);
dinfo([dinfo.isdir]) = []; %get rid of all folders
or
dinfo = dir(selpath);
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %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. https://serverfault.com/questions/406229/ensuring-a-repeatable-directory-ordering-in-linux
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 .

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags