List only top directories

8 次查看(过去 30 天)
Dear Matlab_User
May I know the best approach to list down only the first level directories.
For example, I have a dir with subfolder inside, such as
C:\Users\1st_level_a\2nd_level
C:\Users\1st_level_b\2nd_level
C:\Users\1st_level_c\2nd_level\3rd_level.
However, I am only interested to obtain the url name for the 1st_level,
C:\Users\1st_level_a
C:\Users\1st_level_b
C:\Users\1st_level_c
I tried using the genpath (), however, genpath return me the sub dir as well.
Any suggestion is most welcome,
Cheers,
Rodney

采纳的回答

balandong
balandong 2017-4-17
I found a dirty workaround, but I welcome if there are any more efficient script to replace my method, thansk
start_path = fullfile(matlabroot, 'C:\Users\');
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
list=dir(topLevelFolder); %get info of files/folders in current directory
dirnames={list([list.isdir]).name};
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames)));
for i = 1:length (dirnames)
s (i) = strcat (topLevelFolder,'\', dirnames (i))
end

更多回答(1 个)

Image Analyst
Image Analyst 2017-4-17
Try this:
startingFolder = 'C:/windows/media';
files = dir(startingFolder)
folderIndexes = [files.isdir]
folders = {files(folderIndexes).name} % Note, first two are . and ..
folders = folders(3:end) % Ignore . and .., if you want.

类别

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