필터 지우기
필터 지우기

How to count how many sub folders under a folder

조회 수: 87 (최근 30일)
Jason
Jason 2016년 3월 4일
편집: Stephen23 2023년 11월 24일
In a folder, there are many sub folder, how do we count how many sub folder they are?
  댓글 수: 1
Stephen23
Stephen23 2016년 3월 4일
편집: Stephen23 2022년 9월 3일
Note that KSSV's answer actually counts the total contents of a folder, including any files.
See also:
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"

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

답변 (2개)

KSSV
KSSV 2016년 3월 4일
go the folder...and use
k = dir ;
N = length(k)-2 ;
N should be the number of sub folders.
  댓글 수: 7
Jason
Jason 2016년 3월 4일
Thank you. But his solution work pretty well for me. I just want to check how many items their, I do not care if it is directory or not.
Thomas Carter
Thomas Carter 2022년 3월 13일
This answer also includes any files. So I don't think it is the answer

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


Stephen23
Stephen23 2022년 3월 14일
The correct answer would be something like this:
P = 'absolute or relative path to the folder';
S = dir(P);
N = nnz(~ismember({S.name},{'.','..'})&[S.isdir])

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by