Borrar filtros
Borrar filtros

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

772 visualizaciones (últimos 30 días)
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 comentario
Stephen23
Stephen23 el 24 de Nov. de 2023

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 15 de Dic. de 2014
Editada: Image Analyst el 1 de Dic. de 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 comentarios
Image Analyst
Image Analyst el 2 de Dic. de 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'));

Iniciar sesión para comentar.

Más respuestas (2)

John
John el 15 de Dic. de 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 comentarios

Iniciar sesión para comentar.


Paulo Abelha
Paulo Abelha el 19 de Oct. de 2018
Editada: Paulo Abelha el 19 de Oct. de 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 comentarios
Svetlana Piner
Svetlana Piner el 1 de Dic. de 2021
Thanks to Paulo for a nice little function easy to drop in the code. Super nicely done :)
Image Analyst
Image Analyst el 1 de Dic. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by