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

743 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2023-11-24

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2014-12-15
编辑:Image Analyst 2021-12-1
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 个评论
Image Analyst
Image Analyst 2022-12-2
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'));

请先登录,再进行评论。

更多回答(2 个)

John
John 2014-12-15
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 个评论

请先登录,再进行评论。


Paulo Abelha
Paulo Abelha 2018-10-19
编辑:Paulo Abelha 2018-10-19
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 个评论
Image Analyst
Image Analyst 2021-12-1
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

请先登录,再进行评论。

类别

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