function [out, extname] = getdirfile_long(dirname,ext,option)
%%  function [out extname] = getdirfile_long(dirname,ext,option)
%%%%%%%% function [out extname] = getdirfile_long(dirname,ext,option)
% Input:
%     dirname : path to your data folder
%     ext     : the strings that the file/folder (which you expected) names involves
%     options : 
%        Dir_all  - find all directories
%        Dir_last - find directories in which no other folders exist 
%        Onlyfile - find all files 
%        All      - find all directories and files (this is the default)
% Output:
%     out    : all path
%     extname: all names

% This function employs recursion algorithm to find files or folders you expected, and is much 
% convenient especially in the case that your files were disorganized.

%%%%% Examples
%%%%%   1.     out = getdirfile_long('F:\','.nii')
%%%%%        Find all directories and files that contain '.nii' under directory 'F:\'
%%%%%   2.     out = getdirfile_long('F:\','.nii','Onlyfile')
%%%%%        Find all files that contain '.nii' under directory 'F:\'
%%%%%   3.      out = getdirfile_long('F:\','')
%%%%%        Find all directories and files under 'F:\'

% Original Version  by Zhiliang Long, 2012
% History:
%        20/09/2014  - re-programmed by Zhiliang Long
%%
if nargin < 2
    out = []; extname = [];
    fprintf('\n Input arguments Not enough!');
    return;
end
if nargin <3
    option = 'All';
elseif ~strcmp(option,'Onlyfile') && ~strcmp(option,'Dir_all') && ~strcmp(option,'Dir_last') && ~strcmp(option,'All')
    error('option: Onlyfile, Dir_all, Dir_last, All\n');
end

dirc = dir(dirname);
ndirc = length(dirc(:));
out = cell(0,0); extname = cell(0,0);
for kk=1:ndirc
    if isequal(dirc(kk).name,'.')
    else
        if isequal(dirc(kk).name,'..')
        else
            if dirc(kk).isdir
                
                if isequal(option,'Dir_all') || isequal(option,'All')
                    if isequal(ext,'') || ~isempty(strfind(dirc(kk).name,ext))
                        out{length(out(:))+1} = fullfile(dirname,dirc(kk).name,filesep);
                        extname{length(extname(:))+1} = '';
                    end
                elseif isequal(option,'Dir_last')
                    Underdir = dir([dirname,filesep,dirc(kk).name]);
                    if length(Underdir) < 3
                        Nonedir = 0;
                    else
                        Nonedir = zeros(length(Underdir)-2,1);
                        for jj=1:length(Underdir)-2; Nonedir(jj) = Underdir(jj+2).isdir; end;
                    end
                    if ~any(Nonedir)% are there any dirs under dirc(kk).name
                        if isequal(ext,'') || ~isempty(strfind(dirc(kk).name,ext))
                            out{length(out(:))+1} = fullfile(dirname,dirc(kk).name,filesep);
                            extname{length(extname(:))+1} = '';
                        end
                    end
                end
                
                pathname = fullfile(dirname,dirc(kk).name);
                [tout, textname] = getdirfile_long(pathname,ext,option);
                nlen = length(tout(:));
                nb = length(out(:))+1;
                ne = nb+nlen-1;
                out(nb:ne) = tout(:);
                extname(nb:ne) = textname(:);
            else
                if strcmp(option,'Onlyfile') || strcmp(option,'All')
                    if isequal(ext,'') || ~isempty(strfind(dirc(kk).name,ext))
                        out{length(out(:))+1} = fullfile(dirname,dirc(kk).name);
                        extname{length(extname(:))+1} = dirc(kk).name;
                    end
                end
            end
        end
    end
end
out = out'; extname = extname';
end