%% 复制文件夹结构
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