0

I have a directory which has 3 folders. Each folder of these 3 folders has another number of folders. And in each folder of these folders there are a list of files which I want to run code on each of these file. So for example: its like this: MainFolder has SubFolder1, SubFolder2. SubFolder1 has SubSubFolder1,SubSubFolder2,SubSubFolder3. SubFolder2 has SubSubFolder1,SubSubFolder2. Each of the SunSubFolders has a number of files. I want a script that I give it the MainFolder Path and it goes through each subfolder and subsubfolder and do operations on the files in this subsubfolder and saves the workspace by the name of this subsubfolder. So in the above example, after doing some work on the files in the SubSubFolder1 the resulted workspace will be saved in a location with the name SubSubFolder1.mat.

Please I'm asking if anyone could assist me as this is fairly urgent for me. Many Thanks for your kind assistance and consideration.

Update:

I've done it but another problem rose up in which when I access the files in the SubSubFolders and try to make operation it says "The file '[00000000].pgm' could not be opened because: No such file or directory". How to solve this issue?

this is my code:

D = rdir('Hussein/*/*');           %// List of all sub-directories
for k = 1:length(D)
    currpath = D(k).name;                 %// Name of current directory
    [pathstr, name, ext] = fileparts(currpath);
    %// Operate on all .txt files in currpath
    l = dir(fullfile(currpath, '*.pgm')); %// Listing of all relevant files
    filenames={l.name}';

    nfiles=length(filenames)
    %images=zeros(240, 320, 1000);
    idx=1;

    strtidx=1;
    endidx=nfiles;
    step=1;

    waitbar(0);
    for i=strtidx:step:endidx
        fn=fullfile('', filenames{i});
        tmp=padarray(ut_pgmread(fn), 1, 'post');
        %figure(1); imagesc(tmp); colormap(jet(4096)); colorbar;

        images(:, :, idx)=tmp; idx=idx+1;

        waitbar(i/nfiles);
    end

    close(findall(0,'Tag','TMWWaitbar'));
    name='/Volumes/Untitled/work/'+name;
    save (name, '-v7.3');

    %for m = 1:length(F)
     %   currfile = F(m).name;             %// Name of current file

        %// Do something with currfile...
    %end

    %// Write output (if any) in currpath...
end;
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tak
  • 3,536
  • 11
  • 51
  • 93
  • just search in SO and you'll have almost the full answer: http://stackoverflow.com/questions/8748976/list-the-subfolders-in-a-folder-matlab-only-subfolders-not-files –  Apr 09 '13 at 14:29
  • 1
    @Robocop This is not a recursive directory listing, so it's not a duplicate. – Eitan T Apr 09 '13 at 14:46
  • @EitanT I've updated the question, so if you could please check it. – Tak Apr 09 '13 at 16:23
  • 1
    @EitanT Thanks Eitan, I was able to solve it using cd – Tak Apr 09 '13 at 16:56

1 Answers1

3

It seems that you're looking for a recursive version of dir, so you might find the enhanced rdir tool from the MATLAB File Exchange useful for your purposes. Using enhanced rdir, your code would look something along these lines:

%// List all sub-directories under MainFolder recursively
D = rdir('MainFolder\**\*.');             %// List of all sub-directories
for k = 1:length(D)
    currpath = D(k).name;                 %// Name of current directory

    %// Operate on all .txt files in currpath
    F = dir(fullfile(currpath, '*.txt')); %// Listing of all relevant files
    for m = 1:length(F)
        currfile = F(m).name;             %// Name of current file

        %// Do something with currfile...
    end

    %// Write output (if any) in currpath...
end;
Eitan T
  • 32,660
  • 14
  • 72
  • 109