How to get all files under a specific directory in MATLAB?

I need to get all these files under D:\dic and D:\dic over them for further processing separately.

Does MATLAB support this kind of operation?

This can be done in other scripts such as PHP, Python ...

+90
file directory file-io recursion matlab
Apr 16 2018-10-16T00:
source share
8 answers

Update: Given that this post is quite old, and I modified this utility for my own use during this time, I thought I should post a new version. My latest code can be found on the MathWorks File Exchange : dirPlus.m . You can also get the source from GitHub .

I have made a number of improvements. Now it gives you options for adding the full path or returning only the file name (included in Doresoom and Oz Radiano ) and apply the regular expression pattern to the file names (entered from Peter D ). In addition, I added the ability to apply a validation function to each file, allowing you to select them based on criteria other than their names (for example, file size, contents, creation date, etc.).




NOTE. . In newer versions of MATLAB (R2016b and later), the dir function has a recursive search capability! Thus, you can do this to get a list of all *.m files in all subfolders of the current folder:

 dirData = dir('**/*.m'); 



Old code: (for posterity)

Here is a function that searches recursively through all subdirectories of a given directory, collecting a list of all the file names found by it:

 function fileList = getAllFiles(dirName) dirData = dir(dirName); %# Get the data for the current directory dirIndex = [dirData.isdir]; %# Find the index for directories fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files if ~isempty(fileList) fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files fileList,'UniformOutput',false); end subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories %# that are not '.' or '..' for iDir = find(validIndex) %# Loop over valid subdirectories nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles end end 

After saving the above function somewhere in your MATLAB path, you can call it like this:

 fileList = getAllFiles('D:\dic'); 
+125
Apr 16 2018-10-16T00:
source share

You are looking for dir to return the contents of the directory.

To iterate over the results, you can simply do the following:

 dirlist = dir('.'); for i = 1:length(dirlist) dirlist(i) end 

This should give you the output in the following format, for example:

 name: 'my_file' date: '01-Jan-2010 12:00:00' bytes: 56 isdir: 0 datenum: [] 
+23
Apr 16 2018-10-16T00:
source share

I used the code mentioned in this excellent answer and expanded it to support 2 additional parameters that I need in my case. Parameters are file extensions for filtering and a flag indicating whether the full path to the file name should be concatenated or not.

I hope this is clear enough and someone will find it useful.

 function fileList = getAllFiles(dirName, fileExtension, appendFullPath) dirData = dir([dirName '/' fileExtension]); %# Get the data for the current directory dirWithSubFolders = dir(dirName); dirIndex = [dirWithSubFolders.isdir]; %# Find the index for directories fileList = {dirData.name}'; %'# Get a list of the files if ~isempty(fileList) if appendFullPath fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files fileList,'UniformOutput',false); end end subDirs = {dirWithSubFolders(dirIndex).name}; %# Get a list of the subdirectories validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories %# that are not '.' or '..' for iDir = find(validIndex) %# Loop over valid subdirectories nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path fileList = [fileList; getAllFiles(nextDir, fileExtension, appendFullPath)]; %# Recursively call getAllFiles end end 

Example to run the code:

 fileList = getAllFiles(dirName, '*.xml', 0); %#0 is false obviously 
+13
Oct 19 '14 at 9:19
source share

You can use regexp or strcmp to fix it . and .. Or you can use the isdir field if you only need the files in the directory, not the folders.

 list=dir(pwd); %get info of files/folders in current directory isfile=~[list.isdir]; %determine index of files vs folders filenames={list(isfile).name}; %create cell array of file names 

or combine the last two lines:

 filenames={list(~[list.isdir]).name}; 

List of listings in the directory, excluding. and..

 dirnames={list([list.isdir]).name}; dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames))); 

From now on, you will have to throw the code in a nested loop and continue searching in each subfolder until your dirnames return an empty cell for each subdirectory.

+8
Apr 16 '10 at 2:59 p.m.
source share

This answer does not directly answer the question, but may be a good solution outside the field.

I reviewed the gnovice solution, but I want to offer another solution: use the system-specific command of your operating system:

 tic asdfList = getAllFiles('../TIMIT_FULL/train'); toc % Elapsed time is 19.066170 seconds. tic [status,cmdout] = system('find ../TIMIT_FULL/train/ -iname "*.wav"'); C = strsplit(strtrim(cmdout)); toc % Elapsed time is 0.603163 seconds. 

Positive:

  • Very fast (in my case for a database of 18,000 files on Linux).
  • You can use well-tested solutions.
  • You do not need to learn or invent new syntax to select *.wav files.

Negative:

  • You are not system independent.
  • You rely on a single line, which can be difficult to parse.
+7
Apr 02 '14 at 8:31
source share

I do not know for this a single-function method, but you can use genpath to recombine only a list of subdirectories. This list is returned as a string of directories with semicolons, so you have to separate it using strread, i.e.

dirlist = strread(genpath('/path/of/directory'),'%s','delimiter',';')

If you do not want to include this directory, delete the first dirlist entry, i.e. dirlist(1)=[]; because it is always the first record.

Then get a list of files in each directory with a looped dir .

 filenamelist=[]; for d=1:length(dirlist) % keep only filenames filelist=dir(dirlist{d}); filelist={filelist.name}; % remove '.' and '..' entries filelist([strmatch('.',filelist,'exact');strmatch('..',filelist,'exact'))=[]; % or to ignore all hidden files, use filelist(strmatch('.',filelist))=[]; % prepend directory name to each filename entry, separated by filesep* for f=1:length(filelist) filelist{f}=[dirlist{d} filesep filelist{f}]; end filenamelist=[filenamelist filelist]; end 

filesep returns the directory separator for the platform on which MATLAB is running.

This gives you a list of file names with full paths in the filenamelist of the cell array. I do not know how to do that.

+3
Apr 16 '10 at 15:08
source share

This is a convenient function for getting file names with the specified format (usually .mat ) in the root folder!

  function filenames = getFilenames(rootDir, format) % Get filenames with specified `format` in given `foler` % % Parameters % ---------- % - rootDir: char vector % Target folder % - format: char vector = 'mat' % File foramt % default values if ~exist('format', 'var') format = 'mat'; end format = ['*.', format]; filenames = dir(fullfile(rootDir, format)); filenames = arrayfun(... @(x) fullfile(x.folder, x.name), ... filenames, ... 'UniformOutput', false ... ); end 

In your case, you can use the following snippet :)

 filenames = getFilenames('D:/dic/**'); for i = 1:numel(filenames) filename = filenames{i}; % do your job! end 
+1
Sep 10 '17 at 11:03 on
source share

With a small change, but almost a similar approach, to get the full file path of each subfolder

 dataFolderPath = 'UCR_TS_Archive_2015/'; dirData = dir(dataFolderPath); %# Get the data for the current directory dirIndex = [dirData.isdir]; %# Find the index for directories fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files if ~isempty(fileList) fileList = cellfun(@(x) fullfile(dataFolderPath,x),... %# Prepend path to files fileList,'UniformOutput',false); end subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories %# that are not '.' or '..' for iDir = find(validIndex) %# Loop over valid subdirectories nextDir = fullfile(dataFolderPath,subDirs{iDir}); %# Get the subdirectory path getAllFiles = dir(nextDir); for k = 1:1:size(getAllFiles,1) validFileIndex = ~ismember(getAllFiles(k,1).name,{'.','..'}); if(validFileIndex) filePathComplete = fullfile(nextDir,getAllFiles(k,1).name); fprintf('The Complete File Path: %s\n', filePathComplete); end end end 
0
Jan 02 '18 at 12:02
source share



All Articles