Is there any way to list all folders ONLY in the level directly below a selected directory?

858 views (last 30 days)
I want to generate a list of all of the subfolders within a directory. I was using genpath for this. Unfortunately, each of these subfolders also has 4 subfolders of tehir own, and I don't want them included in this list.
Is there any command that can list the folders only one level below the directory I indicate?
  1 Comment
Stephen23
Stephen23 on 24 Nov 2023

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 15 Dec 2014
Edited: Image Analyst on 1 Dec 2021
John, simply use dir():
topLevelFolder = pwd; % or whatever, such as 'C:\Users\John\Documents\MATLAB\work'
% Get a list of all files and folders in this folder.
files = dir(topLevelFolder);
% Get a logical vector that tells which is a directory.
dirFlags = [files.isdir];
% Extract only those that are directories.
subFolders = files(dirFlags); % A structure with extra info.
% Get only the folder names into a cell array.
subFolderNames = {subFolders(3:end).name} % Start at 3 to skip . and ..
% Optional fun : Print folder names to command window.
for k = 1 : length(subFolderNames)
fprintf('Sub folder #%d = %s\n', k, subFolderNames{k});
end
  10 Comments
Image Analyst
Image Analyst on 2 Dec 2022
Did you try to modify the file I attached. It's just a simple one to use ** to included subfolders. Here is the code:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 22;
markerSize = 20;
% Copies all the files from one folder to another folder.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format compact;
% Define input and output folders.
% CHANGE THESE FOLDER NAMES!!!!!!
topLevelFolder = pwd;
outputFolder = uigetdir(pwd);
if strcmp(outputFolder, topLevelFolder)
errorMessage = sprintf('Error: the output folder must be different than the input folder');
uiwait(warndlg(errorMessage));
return;
end
% Check to see that both folders exist.
if ~isfolder(topLevelFolder)
errorMessage = sprintf('Error: The following input folder does not exist:\n%s', topLevelFolder);
uiwait(warndlg(errorMessage));
return;
end
if ~isfolder(outputFolder)
errorMessage = sprintf('Error: The following output folder does not exist:\n%s', outputFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of files to copy in inputFolder and all subfolders.
filePattern = fullfile(topLevelFolder, '**/*.*'); % All files.
% filePattern = fullfile(topLevelFolder, '**/*.m'); % m-files.
fileNamesToTransfer = dir(filePattern);
numFiles = length(fileNamesToTransfer);
% Do the copying.
for k = 1 : numFiles
% Get the base file name.
baseFileName = fileNamesToTransfer(k).name;
inputFolder = fileNamesToTransfer(k).folder;
% Create the full input and output filenames.
fullInputFileName = fullfile(inputFolder, baseFileName);
fullOutputFileName = fullfile(outputFolder, baseFileName);
fprintf(1, 'Now copying file #%d of %d: %s to %s\n', ...
k, numFiles, fullInputFileName, fullOutputFileName);
copyfile(fullInputFileName, fullOutputFileName);
end
uiwait(msgbox('Done copying files!', 'modal'));

Sign in to comment.

More Answers (2)

John
John on 15 Dec 2014
Thanks, this is great.
I ran into another problem. It seems to list two directories I don't want, '.' and '..", whcih apparently correspond to the folders containing these subfolders.
Is there any way to make it so that it removes those entries? I want to make this script as intuitive as possible so that it needs minimal modification later on.
  5 Comments

Sign in to comment.


Paulo Abelha
Paulo Abelha on 19 Oct 2018
Edited: Paulo Abelha on 19 Oct 2018
function [subDirsNames] = GetSubDirsFirstLevelOnly(parentDir)
% Get a list of all files and folders in this folder.
files = dir(parentDir);
% Get a logical vector that tells which is a directory.
dirFlags = [files.isdir];
% Extract only those that are directories.
subDirs = files(dirFlags);
subDirsNames = cell(1, numel(subDirs) - 2);
for i=3:numel(subDirs)
subDirsNames{i-2} = subDirs(i).name;
end
end
  7 Comments
Image Analyst
Image Analyst on 1 Dec 2021
Here is a vectorized version (no for loop):
function [subDirsNames] = GetSubDirsFirstLevelOnly(parentDir)
% Get a list of all files and folders in this folder.
files = dir(parentDir);
% Get a logical vector that tells which is a directory.
dirFlags = [files.isdir];
% Extract only those that are directories.
subDirs = files(dirFlags); % A structure with extra info.
% Get only the folder names into a cell array.
subDirsNames = {subDirs(3:end).name};
end

Sign in to comment.

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!