MATLAB 只复制文件夹名字及结构,不复制文件夹内容

%% 复制文件夹结构
clear all;close all;clc
filepath = uigetdir('*.*','请选择要复制的第一级文件夹名称');
if filepath==0
    warndlg('请选择要复制的第一级文件夹名称');
end

directory = filepath ;%要复制的文件夹第一级目录名称
filepathSplit=split(filepath,'\');
splitPart=['\',char(filepathSplit(end))];%将第一级目录名称从路径中取出
S = regexp(filepath, splitPart, 'split');%以第一级目录名称为分割依据

answer=inputdlg('输入创建的文件名称','创建保存的文件夹名称');
filepathHead=strcat(char(S(1,1)),char(answer(1,1)));
 
folder{1}=directory;
flag=1; %1 when there are folders havenot be searched,0 otherwise
while flag
    currfolders=folder;
    folder={};
   
    for m=1:1:length(currfolders)
        direc=currfolders{m};
              
        allfiles=dir(direc);
        %the number of all the files in the current searching folder
        L=length(allfiles);
        %the number of folders to be searched on this class
        k=length(folder);
        for i=1:1:L
            if allfiles(i).isdir & (~strcmp(allfiles(i).name,'.')) & ~strcmp(allfiles(i).name,'..')
                k=k+1;
                folder{k}=[direc,filesep,allfiles(i).name];
                folder_name=[direc,filesep,allfiles(i).name];
                remain=folder_name;
                for j=1:length(filepathSplit)
                [token, remain] = strtok(remain,'\');
                end
                new_folder_name = strcat(filepathHead,remain);
                mkdir(new_folder_name);
            end
        end
    end
   
    %if there are no folders that havenot searched yet,flag=0 so the loop
    %will be ended
    if ~length(folder)
        flag=0;
    end
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值