List files within a folder

11 ビュー (過去 30 日間)
Inês Mendes
Inês Mendes 2015 年 6 月 4 日
コメント済み: Image Analyst 2015 年 6 月 4 日
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
  1 件のコメント
Sean de Wolski
Sean de Wolski 2015 年 6 月 4 日
Define "it's not working"

サインインしてコメントする。

回答 (1 件)

Image Analyst
Image Analyst 2015 年 6 月 4 日
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 件のコメント
Inês Mendes
Inês Mendes 2015 年 6 月 4 日
can you give an example? i am not being able to use it!
i would be much appreciated!
Image Analyst
Image Analyst 2015 年 6 月 4 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFilename Construction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by