What are the extra subdirectories returned by dir function?

조회 수: 21 (최근 30일)
John
John 2018년 11월 19일
댓글: Guillaume 2019년 4월 4일
When using the dir function, the first two elements returned have name field values of '.' and '..', which are determined to be directories by the dir function. However, these directories are not created nor do they appear in the directory window. Where do these directories come from?
  댓글 수: 1
Guillaume
Guillaume 2019년 4월 4일
the first two elements returned have name field values of '.' and '..'
They are not always the first two elements. Assuming that . and .. are always the first two elements returned by dir will lead to bugs. See my answer for more details.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2018년 11월 19일
편집: Matt J 2018년 11월 19일
If you do
>> cd ..
and
>> cd .
you will see that they are aliases for the parent folder and the current folder, respectively.

추가 답변 (1개)

Guillaume
Guillaume 2018년 11월 19일
편집: Guillaume 2018년 11월 19일
<rant mode on> They're absurd hold overs from a long gone era (DOS) where knowing that the current directory had a parent was somewhat useful. . always refers to the current directory, .. to the parent of the current directory. Nowadays, it's just clutter and I wish dir didn't return them as inevitably, you need to filter them out. Modern file handling code (e.g. .Net System.IO.Directory.GetDirectories) only return the list of actual folders.<end rant>
Note that the safe way to filter out these entries is with
filefolderlist = dir(somepath);
filterelist = filefolderlist(~ismember({filefolderlist.name}, {'.', '..'}));
Do not remove the first two entries as . and .. are not always first. Folders starting with a + for example will be listed before them.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by