Clear Filters
Clear Filters

List only top directories

8 views (last 30 days)
balandong
balandong on 17 Apr 2017
Answered: Image Analyst on 17 Apr 2017
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

Accepted Answer

balandong
balandong on 17 Apr 2017
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

More Answers (1)

Image Analyst
Image Analyst on 17 Apr 2017
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.

Categories

Find more on Search Path in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!