List files within a folder

11 views (last 30 days)
Inês Mendes
Inês Mendes on 4 Jun 2015
Commented: Image Analyst on 4 Jun 2015
Hi guys,
I am trying to list all the files i have inside a folder but i am having some difficulties. First of all, i listed the names of all the folders i have inside that directory. Now, i am trying to list the files within those folders. I used the code bellow but it´s not working :X
handles.diretorio='H:\Tese\ferramenta\excel\';
handles.lista=dir(fullfile(handles.diretorio));
handles.C=get(handles.popupmenu5,'String');
handles.C=handles.lista;
handles.listaC=dir(fullfile(strcat(handles.diretorio,handles.C)));
Can anyone help?
Inês

Answers (1)

Image Analyst
Image Analyst on 4 Jun 2015
You can use genpath(). That's what I do in my attached demo. It will recurse into all subfolders and find the names of all the files in all those folders.
  2 Comments
Inês Mendes
Inês Mendes on 4 Jun 2015
can you give an example? i am not being able to use it!
i would be much appreciated!
Image Analyst
Image Analyst on 4 Jun 2015
Try this code. I changed it a bit and it's attached again.
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all files in
% that folder and all of its subfolders.
% Similar to imageSet() function in the Computer Vision System Toolbox: http://www.mathworks.com/help/vision/ref/imageset-class.html
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox');
if ~exist(start_path, 'dir')
start_path = matlabroot;
end
% Ask user to confirm or change.
uiwait(msgbox('Pick a starting folder on the next window that will come up.'));
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get ALL files.
filePattern = sprintf('%s/*.*', thisFolder);
baseFileNames = dir(filePattern);
% % Get m files.
% filePattern = sprintf('%s/*.m', thisFolder);
% baseFileNames = dir(filePattern);
% % Add on FIG files.
% filePattern = sprintf('%s/*.fig', thisFolder);
% baseFileNames = [baseFileNames; dir(filePattern)];
% Now we have a list of all files in this folder.
numberOfImageFiles = length(baseFileNames);
if numberOfImageFiles >= 1
% Go through all those files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing file %s\n', fullFileName);
end
else
fprintf(' Folder %s has no files in it.\n', thisFolder);
end
end

Sign in to comment.

Categories

Find more on Filename Construction 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!