dir() gives extra information

1 visualización (últimos 30 días)
monkey_matlab
monkey_matlab el 11 de Nov. de 2016
Respondida: Walter Roberson el 11 de Nov. de 2016
Hello, I wanted to just get the number of subfolders from a specific main folder. The main folder contains files, together with sub-folders. How do I get just the number of folders?
If I use
size(dir())
I get everything in the main folder. How to get just the number of sub-folders?

Respuesta aceptada

Walter Roberson
Walter Roberson el 11 de Nov. de 2016
MyFolderInfo = dir('myfolder');
mask = ismember({MyFolderInfo.name}, {'.', '..'});
MyFolderInfo(mask) = []; %get rid of . and .. directories
num_subfolder = sum( [MyFolderInfo.isdir] );

Más respuestas (1)

Image Analyst
Image Analyst el 11 de Nov. de 2016
Editada: Image Analyst el 11 de Nov. de 2016
Look up isdir() in the help. Hopefully it's obvious what to do from there.
Actually dir() tells you:
MyFolderInfo = dir('myfolder')
MyFolderInfo =
5×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
See? The structure has an "isdir" field that flags whether or not the file returned is a folder or not.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by