Search code examples
matlabdirectorypattern-matchingfilepathsubdirectory

Cannot find sub-directories


I'm trying to identify a certain subdirectory pattern in my script. The issue I'm getting is that the directories are not being Identified correctly. Here is my current code:

parentDir = '/data/home1/fL/user/path/to/subdirectories/'
totalFiles = subdir(fullfile(parentDir, '/*.7'));

name = {totalFiles.name}' % cells containing directories with the .7 ext this is a 118*1 cell
numTotalFiles=length(name); % =118

% this section is supposed to sort through all the subdirectories paths that
% have the pattern A_G/*.7 and p_G/*7 in their pathname but fails to do so. 

for i=1:numTotalFiles
  patternsplit = totalFiles(i).name
  str = ["*/A_G/*.7","*/p_G/*.7"]
  ptrn = ["A_G","p_G"]
  pattern = patternsplit(startsWith(str,ptrn))
  found = pattern
end

I expect the output to be a list of subdirectories containing the patterns A_G/*.7 and p_G/*.7. I'm thinking this may be a 44x1 cell containing all the subdirectories names with these patterns.


Solution

  • The following solution uses another approach than your previous one, which first listed all filenames and than tried to find matching patterns in the list.

    This approach uses recursion (this task calls for recursion, in my opinion). The major work is made a function that scans the content of the current directory for .7 files. If the current directory's name is either A_G or p_G, it returns the file names. Then, it calls itself with the subdirectories.

    Note that this code generates some files and directories for testing!

    % Let's create a directory structure with some files with extension .7
    
    mkdir ('foo/bar/baz/A_G');
    mkfile('foo/bar/baz/A_G', 1);
    mkdir ('foo/bar/baz/p_G');
    mkfile('foo/bar/baz/p_G', 2);
    
    mkdir ('foo/bar/baz/test');
    mkfile('foo/bar/baz/test', 99); % should not be found since in directory `test`
    
    
    mkdir ('foo/bar/p_G');
    mkfile('foo/bar/p_G', 3);
    mkdir ('foo/bar/A_G');
    mkfile('foo/bar/A_G', 4);
    
    mkdir ('foo/p_G');
    mkfile('foo/p_G', 5);
    mkfile('foo/p_G', 6);
    
    
    % Here it starts
    
    parent = '.'; % My parent folder is my current folder
    
    l_find_files(parent, false)
    
    
    function mkfile(d, n)
    % just a helper function to generate a file named `<n>.7` in the dirctory <d> 
        fid = fopen(sprintf('%s%s%d.7',d,filesep,n),'w');
        fclose(fid);
    end
    
    
    function res = l_find_files(d, is_candidate)
    % recursively scans a directory <d> for *.7 files.
    % if is_candidate == true, returns them.
    % is_candidate should be set to true if the name of the directory is one of
    % {'A_G', 'p_G'}
    
    
        files = dir(d);
        res = {};
        if is_candidate
            % get files with extension .7
            % 
            % Instead of using the above `files`, I rescan the directory. Using
            % `files` instead might be faster
    
            ext7 = dir(fullfile(d,'*.7'));
    
            res = arrayfun(@(x)fullfile(d, x.name), ext7, 'UniformOutput', false);
        end
    
        % get the indices of the subdirectories
        issub = [files(:).isdir];
        subdirs = {files(issub).name};
    
        % call this function for the subdirectories
        for sd = subdirs
            if ~any(strcmp(sd{1}, {'.', '..'})) % omit . and ..
                result = l_find_files(fullfile(d, sd{1}), any(strcmp(sd{1}, {'A_G', 'p_G'})));
                res = [res(:)' result(:)'];
            end
        end
    
    end