matlab遍历文件并完成文件移动

只能遍历一层的code:

path = 'E:\car\20160918MA\data\image\';  
pth = 'E:\car\20160918MA\data\imgtmp\';
fileExt = '*.jpg';  
files = dir(fullfile(path,fileExt));  
len = size(files);  
for i=1:len  
fileName = strcat(path,files(i,1).name)
copyfile(fileName, pth);   
end; 

能遍历多层code:

% <span style="font-family: Arial, Helvetica, sans-serif;">RangTraversal script</span>
function [ mFiles ] = RangTraversal( strPath )  
    %定义两数组,分别保存文件和路径  
    mFiles = cell(0,0);  
    mPath  = cell(0,0);  
      
    mPath{1}=strPath;  
    [r,c] = size(mPath);  
    while c ~= 0  
        strPath = mPath{1};  
        Files = dir(fullfile( strPath,'*.*'));  
        LengthFiles = length(Files);  
        if LengthFiles == 0  
            break;  
        end  
        mPath(1)=[];  
        iCount = 1;  
        while LengthFiles>0  
            if Files(iCount).isdir==1  
                if Files(iCount).name ~='.'  
                    filePath = [strPath  Files(iCount).name '/'];  
                    [r,c] = size(mPath);  
                    mPath{c+1}= filePath;  
                end  
            else  
                filePath = [strPath  Files(iCount).name];  
                [row,col] = size(mFiles);  
                mFiles{col+1}=filePath;  
            end  
  
            LengthFiles = LengthFiles-1;  
            iCount = iCount+1;  
        end  
        [r,c] = size(mPath);  
    end  
  
    mFiles = mFiles';  
end 


 %demo

%% The directory of your files  
str = 'E:/car/20160918MA/data/label/';  
pth = 'E:\car\20160918MA\data\mark\';  
%% The use of depth-first walk  
%mFiles = [];  
%[mFiles, iFilesCount] = DeepTravel(str,mFiles,0)  
%mFiles = mFiles';  
  
%% The use of breadth first walk  
mFiles2 = RangTraversal(str)  
len = size(mFiles2);  
for i=1:len  
fileName =mFiles2{i};
copyfile(fileName, pth);   
end; 

参考:http://blog.csdn.net/carson2005/article/details/17263083

            http://blog.csdn.net/guoxiaojie_415/article/details/21317323

            http://blog.csdn.net/stpeace/article/details/8230476

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值