omitting pointers when listing folder contents

Asked by Richard on 12 Jun 2012
Latest activity Commented on by Walter Roberson on 12 Jun 2012

In order to list folder contents I use:

TopFolder = dir('E:\');

Say if I had one file within that specific folder MATLAB returns 3 structures where the name of the first two are just pointers i.e. '.' and '..'.

How is it possible to omit these? I currently just omit the first two by:

a = SubFolder(3:end)

then use

a.name 

to obtain the name. Is there a more accurate way of doing this i.e. supported by the documentation?

0 Comments

Richard

Tags

Products

No products are associated with this question.

2 Answers

Answer by Jan Simon on 12 Jun 2012
Accepted answer

It is not documented, that . and .. are the first two elements replied by DIR. Therefore checking the names is safer:

DirList = dir('E:\');
DirName = {DirList.name};
DirList(strcmp(DirName, '.')) = [];
DirList(strcmp(DirName, '..')) = [];

To omit all files and folders starting with a dot:

DirList = dir('E:\');
DirName = {DirList.name};
DirList(strncmp(DirName, '.', 1)) = [];

Now "DirList" is the cleaned list of files and folders.

1 Comment

Walter Roberson on 12 Jun 2012

But will still have any folders whose names did not happen to start with '.'

Jan Simon
Answer by Walter Roberson on 12 Jun 2012
TopFolder = dir('E:\');
TopFolder([TopFolder.isdir]) = [];

0 Comments

Walter Roberson

Contact us