フィルターのクリア

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 Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by