Is there any way to list all folders ONLY in the level directly below a selected directory?
604 次查看(过去 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
2023-11-24
Note that answers and comments which use the indexing 3:end, e.g.
{subFolders(3:end).name} % Start at 3 to skip . and ..
and
subDirsNames = {subDirs(3:end).name};
are fundamentally fragile and should be avoided as general solutions. This has been discussed many times:
https://www.mathworks.com/matlabcentral/answers/1699230-folder-listing-with-dir-on-mac#answer_945260
As Walter Roberson wrote: "That code is wrong on every operating system that MATLAB has ever run on".
The recommended approach is to specify a non-empty DIR search name, or use e.g. ISMEMBER, SETDIFF, or similar:
采纳的回答
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
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 个)
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 个评论
Svetlana Piner
2021-12-1
Thanks to Paulo for a nice little function easy to drop in the code. Super nicely done :)
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 Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!