0

Based on this answer, I'm trying to find all directories and subdirectories that contain a specified string. Right now I've the following code, which show ALL directories and subdirectories (the string pattern is not implemented and that's what I would like to have):

function fileNames = findAllDirectories(directory, wildcardPattern)

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

    files = FileUtils.listFilesAndDirs( File(directory),...
                                        NotFileFilter(TrueFileFilter.INSTANCE),...
                                        DirectoryFileFilter.DIRECTORY);

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

How do I specify to search for a name pattern in the directory/subdirectory names?

For example, if I have the following directory structure:

C:\aaa
C:\aaa\aaa
C:\aaa\bbb
C:\aaa\ccc
C:\aaa\bbb\ccc
C:\aaa\ddd
C:\aaa\ddd\bbb

and that I call findAllDirectories('C:\aaa','ccc'), the result should be :

C:\aaa\ccc
C:\aaa\bbb\ccc
Community
  • 1
  • 1
m_power
  • 3,156
  • 5
  • 33
  • 54

1 Answers1

1

Try using this function, which doesn't use any Java libraries:

function dirPaths = findAllDirectories(baseDirectory, wildcardPattern)

dirPaths = recFindAllDirectories(baseDirectory);

    function matchedDirPaths = recFindAllDirectories(searchPath)
        files = dir(searchPath); % gets a struct array of the files and dirs in the dir.
        files = files(3:end); % removes '.' and '..'
        dirs = files([files.isdir]); % filters the results to directories only.
        dirNames = {dirs.name}; % takes the names of the directories
        matchedNamesIdxs = ~cellfun(@isempty, regexp(dirNames, wildcardPattern)); % applys the pattern search.
        matchedDirPaths = fullfile(searchPath, dirNames(matchedNamesIdxs)); % concats to get a full path to the matched directories.
        for i = 1:length(dirNames)
            currMatchedDirPaths = recFindAllDirectories(fullfile(searchPath, dirNames{i})); % recursively calls the function for the subdirectories.
            matchedDirPaths = [matchedDirPaths currMatchedDirPaths]; % adds the output of the recursive call to the current call's output.
        end
    end

end

With your directory structure, the same call will output the cell array:

'C:\aaa\ccc' 'C:\aaa\bbb\ccc'

Shaked
  • 475
  • 1
  • 3
  • 12
  • It works great. I wanted to use java in order to avoid using recursive function and to have a simpler code to understand. Do you know how to implement this in Java? – m_power Sep 08 '14 at 19:19
  • It's been to long since I wrote in Java, sorry. If you have any questions about this code feel free to ask. The recursive call is just for easy implementation, though I think in this case it shouldn't be hard to reimplement this function to be non-recursive. – Shaked Sep 08 '14 at 19:41