function All = getSubfolder(filepath, fileType)
% getSubfolder 
% output: filenames -- without path
%         fullFiles -- with path
%         filePath  -- paths

%   Project: IMPACT
%   Title of Code: getSubfolder
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.



filenames = {};
fullFiles = {};
filePaths = {};

nFilepath = numel(filepath);
if nargin > 1
    switch fileType
        case 'bladed'
            fileExt = '*.$PJ';
        otherwise
            fileExt = fileType;
    end
else fileExt = '*.$PJ';
end

posfileExt = {'*.$PJ','*.int','*.out'};
out = 0;
fileExt = [];
for m = 1:nFilepath
    for n = 1: numel(posfileExt)
        simFiles = dir(fullfile(filepath{m}, posfileExt{n}));
        if ~isempty(simFiles)
            fileExt = posfileExt{n};
            out = 1;
            break;
        end
    end
    if out
        break;
    end
end

if ~isempty(fileExt)
    for m = 1:nFilepath
        simFiles = dir(fullfile(filepath{m}, fileExt));
%         if isempty(simFiles) && nargin<=1
%             simFiles = dir(fullfile(filepath{m}, '*.csv'));
%         end
        if ~isempty(simFiles) % sim files
            filenamesTemp = {simFiles.name};
            nFilenames = numel(filenamesTemp);
            fullFilesTemp = cell(1, nFilenames);
            for n = 1:nFilenames
                fullFilesTemp{n} = fullfile(filepath{m}, filenamesTemp{n});
            end
            
            filenames = [filenames(:); filenamesTemp'];
            fullFiles = [fullFiles(:); fullFilesTemp(:)];
            filePathsTemp = repmat(filepath(m), nFilenames, 1);
            filePaths = [filePaths(:); filePathsTemp];
        end
    end
end


All.filenames = filenames;
All.fullFiles = fullFiles;
All.filePath = filePaths;

% change $pj into $PJ
if ~isempty(All.fullFiles)
    All.fullFiles = changePJ(All.fullFiles);
end
