3

I have the code like this:

          myFolder='C:\Users\abe7rt\Desktop\dat\1';
          filePattern=fullfile(myFolder, '*.txt');
          txtFiles=dir(filePattern); 

Now, dat is a folder that contains "1,2,3" folders and each one of these folders contains 20 TXT files. The previous code is able to get the txt files from 1 folder. Now my question is: is there a way to loop over all the directories?

Fraukje
  • 673
  • 3
  • 20
mecaeng
  • 93
  • 2
  • 8

4 Answers4

4

Yet another possibility, using the apache commons library that comes with MATLAB:

function fileNames = findAllFiles(directory, wildcardPattern)

    import org.apache.commons.io.filefilter.*;
    import org.apache.commons.io.FileUtils;
    import java.io.File;

    files = FileUtils.listFiles( File(directory),...
                                 WildcardFileFilter(wildcardPattern),...
                                 FileFilterUtils.trueFileFilter());

    fileNames = cellfun(@(f) char(f.getCanonicalPath()),...
                        cell(files.toArray()),...
                        'uniformOutput', false);
end

Use e.g. as:

files = findAllFiles('C:\Users\abe7rt\Desktop\dat', '*.txt')

If you'd like to also apply a pattern on the directory-names in which the search should descend, you can simply replace the FileFilterUtils.trueFileFilter() with another WildcardFileFilter.

sebastian
  • 9,526
  • 26
  • 54
  • Is it possible to use this but on folders? Like, is it possible to loop over folder names and not files? Hence, the result would be a list of folders (path) with a specified name? – m_power Sep 10 '14 at 18:03
3

yes there is :)

A very nice function is this one:

by gnovice: getAllFiles

You can use it like this:

fileList = getAllFiles('D:\dic');

Then you just have to get rid of non-txt-files, e.g. by checking the extension within a loop!

Community
  • 1
  • 1
Lucius II.
  • 1,832
  • 12
  • 19
  • But in this case i will not able to control all the data *too much dat.. I have like 20 sub folder? – mecaeng Sep 26 '13 at 09:53
  • well, this is something special... what error-message do you get then? – Lucius II. Sep 26 '13 at 09:55
  • nooo. It works really but I just got all txt in thess folders , so i will not able to control this txt files . – mecaeng Sep 26 '13 at 10:09
  • what do you mean by "control"? you could filter the results afterwards, if there are too many files or if there are folders you dont want to include. – Lucius II. Sep 26 '13 at 11:09
  • Ok .every folder contains 3 floders , and each folder contain like 20 txt files. These txt files starting form 1 to 20 in all folders. – mecaeng Sep 26 '13 at 11:17
  • this answer works very well but you need more errofrt to filter the files (in case the file names were simmilar) – mecaeng Sep 27 '13 at 07:12
2

Well you could do something like

for k=1:3
    myFolder{k}=['C:\Users\abe7rt\Desktop\dat\' num2str(k)];
    filePattern{k}=fullfile(myFolder{k}, '*.txt');
    txtFiles{k}=dir(filePattern{k});
end

You can obviously pre-allocate the sizes of the arrays / cell arrays if performance/memory is an issue.

am304
  • 13,758
  • 2
  • 22
  • 40
  • I did not get how to pre-allocate the sizes of the arrays ? Subscripted assignment dimension mismatch. – mecaeng Sep 26 '13 at 10:00
  • second one myFolder(k)=['C:\Users\abe7rt\Desktop\dat\' num2str(k)]; – mecaeng Sep 26 '13 at 10:04
  • I mean explain more what do you mean by cell array here?? – mecaeng Sep 26 '13 at 10:53
  • See http://www.mathworks.co.uk/help/matlab/cell-arrays.html. I edited my answer to use {k} instead (k) – am304 Sep 26 '13 at 10:55
  • If i want to use the txt file to perfom some tasks. can i use this `txtFiles{k}=dir(filePattern{k}.name);` – mecaeng Sep 26 '13 at 11:32
  • like this `for k=1:3 myFolder{k}=['C:\Users\abe7rt\Desktop\dat\' num2str(k)]; filePattern{k}=fullfile(myFolder{k}, '*.txt'); txtFiles{k}=dir(filePattern{k}.name); end ` – mecaeng Sep 26 '13 at 11:33
1

You can use the recursive version of Matlab's function fileattrib, and then select the txt files with regexp. This solution works for any number of folders and any level of nesting.

[success,message,messageid] = fileattrib('C:\Users\abe7rt\Desktop\dat\*');
[names{1:numel(message)}] = deal(message.Name);
names_txt = names(~cellfun(@isempty, regexp(names,'\.txt$')));

The cell array names_txt contains the full names of all txt files. So names_txt{n} is a string with the full name of the n-th file.

Make sure you add the final \* to the root path.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147