2

I want to have a list of all full sub-folders. The list shall not contain any parent folder.

I get the list of dirs using

dirs = regexp(genpath(basePath),['[^;]*'],'match');

However that functions is really slow. Probably because my folders contain thousands of files.

The removefall of the parent folders is done this way: Is there a possibility to optimize this in terms of code size and speed?

function [ ListOfDirs ] = findsubfolders( basePath )

dirs = regexp(genpath(basePath),['[^;]*'],'match');

index = 0;
for k = 1:numel(dirs)
    currFolder = dirs{k};
    if numel(strrep(currFolder, basePath,'')) ~= 0
        if isempty(strfind(currFolder, 'remove'))
            index = index + 1;
            selectedDirs{index} = currFolder;
        end
    end
end

dirs = selectedDirs;
idx = 0;
for k = 1:numel(dirs)
    currFolder = dirs{k};
    isNotParentFolder = false;
    for s = 1:numel(dirs)
        if s ~= k
            compFolder = dirs{s};
            if numel(strrep(strrep(currFolder, compFolder,''),currFolder,'')) ~= 0
                isNotParentFolder  = true;
            end
        end
        if isNotParentFolder
            idx = idx + 1;
            ListOfDirs{idx} = currFolder;
            break;
        end
    end
end

end
marsei
  • 7,691
  • 3
  • 32
  • 41
Matthias Pospiech
  • 3,130
  • 18
  • 55
  • 76
  • Have you tried http://www.mathworks.co.uk/matlabcentral/fileexchange/41135-folders-sub-folders or http://www.mathworks.co.uk/matlabcentral/fileexchange/1492-subdir-new? – am304 Nov 29 '13 at 11:21
  • The `genpath` approach (and other suggestions?) are basically calling `dir` recursively. This is known to be quite slow (for network drives?). Perhaps this can help: http://stackoverflow.com/questions/6385531/very-slow-dir-in-matlab – Dennis Jaheruddin Nov 29 '13 at 11:24

1 Answers1

2

I suggest you use fileattribs to obtain the file and folder names. This function recursively searches all files and folders within a given base folder, and then you can select only the folders. It may be slow if you have many files or folders; but then it will probably happen with any other approach:

[success,message,messageid] = fileattrib('c:\users\lmendo\Documents\*');
%// final '\*' is needed to make the search recursive
isfolder = [message(:).directory]; %// true for folders, false for files
[folders{1:sum(isfolder)}] = deal(message(isfolder).Name) %// keep folders only
%// folders is a cell array of strings with all folder names

To keep only the deepest folders, use strmatch. It checks if a string (representing a folder) matches the beginning of another string (which would then be its subfolder). This is simpler (and perhaps faster) than your findsubfolders function.

isDeepest = cellfun(@(str) numel(strmatch(str,folders))==1, folders);
%// "==1" because each folder at least matches itself. So 1 match indicates
%// it's deepest, more matches indicates it's not.
deepestFolders = folders(isDeepest); %// keep deepest folders only
%// deepestFolders is a cell array of strings with all deepest-folder names
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147