Filter löschen
Filter löschen

Count another directory subfolder

11 Ansichten (letzte 30 Tage)
Jason
Jason am 9 Mär. 2016
Kommentiert: Jason am 11 Mär. 2016
Now I am in a script under "C:\User\Jason\MATLAB". In this script, I want to count how many subfolder in another directory 'C:\User\Jason\MATLAB\Days'. Now I write the code in the below to count, but only return 17, actually the subfolder is more than 100. Any solution to fix this bug? Thanks.
dir C:\User\Jason\MATLAB\Days'
length(dir)-2

Akzeptierte Antwort

Guillaume
Guillaume am 9 Mär. 2016
You don't assign the output of the first line to anything, so it's a wasted lines. The second line just call dir again but since you don't specify a directory, does it in the current directory (which, it looks like, has 17 files/folders).
Avoid using the command form of functions in scripts. Here is a clean way of doing it:
path = 'C:\User\Jason\MATLAB\Days';
dircontent = dir(path);
numfolders = sum([dircontent.isdir]) - 2; %-2 to account for the stupid '.' and '..' returned by dir
  3 Kommentare
Stephen23
Stephen23 am 9 Mär. 2016
sum([dircontent.isdir])
only counts the subfolders, and ignores any files.
Jason
Jason am 11 Mär. 2016
thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by