function returnFiles=getFiles(baseDir)

function returnFiles=getFiles(baseDir)
% get files in a directory
%input baseDir: is the directory for get Files,it must be a string end with
%'/'
%output returnFiles:is a cell of file paths in the directory
files=dir(baseDir);
%m is the numbers of the files contained in the directory
[m,tmp]=size(files);
%returnFiles is a cell for storing the file paths
%it is set to size of 1 and storing 'Files are:' for the state that there is no files
%in the directory
returnFiles=cell(1);
returnFiles{1}='Files are:';
for i=1:m
    %processing erery file
    current=files(i);
    %get the file name
    file_name=current.name;
    %processing with the state of '.' and '..'
    if strcmp(file_name,'.')==1 || strcmp(file_name,'..')==1      
        continue;
    end         
    if current.isdir==1
        % if it is a directory
        % get the diretory path
        currentDir=strcat(baseDir,strcat(file_name,'/'));
        %processing the sub direcotry
        tmpReturnFiles=getFiles(currentDir);
        %save the files contained in the subdir to the cell of returnFiles
        %processing with the state that there is no file in the subdir
        if strcmp(tmpReturnFiles{1},'Files are:')==0
            %processing with the state that there is no file stored in the
            %cell of returnFiles
            if strcmp(returnFiles{1},'Files are:')==1
                returnFiles=tmpReturnFiles;
            else
                %merge the files in current dir and the files in the subdir
                returnFiles=[returnFiles,tmpReturnFiles];
            end
        end       
    else
        % get the file path of the file
        filePath=strcat(baseDir,file_name);
        %to find that if there is any file stored in the cell
        if strcmp(returnFiles{1},'Files are:')==1
            % if there is no file stored in the cell,than save current file
            % to the first cell
            returnFiles{1}=filePath;
        else
            % if there is any file stored in the cell,than merge
            returnFiles=[returnFiles,{filePath}];
        end
    end   
end 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值