Skip to content

Commit

Permalink
feat(Util): allow file extension filtering
Browse files Browse the repository at this point in the history
Updates FilesInFolder to permit unwanted
file extensions to be filtered out.
  • Loading branch information
ocehugo committed Mar 9, 2021
1 parent 495c601 commit a19ef3b
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions Util/Path/FilesInFolder.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
function [files, folders] = FilesInFolder(path)
% function files = FilesInFolder(path)
function [files, folders] = FilesInFolder(path, exclude_extensions)
% function files = FilesInFolder(path, exclude_extensions)
%
% Return two cells with fullpath of files/folders within
% a path.
% The cells are sorted.
%
% Inputs:
%
% path - a path string
% path [str] - a path string
% exclude_extensions [cell{str}] - Optional: a cell with file
% extensions to exclude.
%
%
% Outputs:
%
Expand All @@ -21,26 +24,21 @@
% assert(any(contains(files,'license.txt')));
% assert(any(contains(files,'toolboxProperties.txt')));
%
% % exclude txt files.
% [files] = FilesInFolder(toolboxRootPath,{'.txt'});
% assert(~any(contains(files,'license.txt')))
%
% author: hugo.oliveira@utas.edu.au
%

% Copyright (C) 2019, Australian Ocean Data Network (AODN) and Integrated
% Marine Observing System (IMOS).
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation version 3 of the License.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.
% If not, see <https://www.gnu.org/licenses/gpl-3.0.en.html>.
% author: hugo.oliveira@utas.edu.au
%
narginchk(1,2)
if nargin==1
exclude_extensions = {};
else
if ~iscellstr(exclude_extensions)
errormsg('Second argument is not a cell of strings')
end
end

dobj = dir(path);
files = cell(0, 0);
Expand All @@ -54,8 +52,17 @@

if ~isfolder
fullpath = fullfile(path, name);
c = c + 1;
files{c} = fullpath;
skip = false;
for j=1:length(exclude_extensions)
if endsWith(fullpath,exclude_extensions{j})
skip = true;
break
end
end
if ~skip
c = c + 1;
files{c} = fullpath;
end
else
not_dots = ~strcmp(name, '.') &&~strcmp(name, '..');

Expand Down

0 comments on commit a19ef3b

Please sign in to comment.