clear;clc
% 设置主目录的路径
mainDir = 'E:\ED\add\original'; % 修改为你的主目录路径
% 获取主目录下所有子文件夹
subfolders = dir(mainDir);
isSub = [subfolders(:).isdir];
subfolderNames = {subfolders(isSub).name}';
subfolderNames(ismember(subfolderNames, {'.', '..'})) = [];
% 遍历每个子文件夹
for i = 1:length(subfolderNames)
subDirPath = fullfile(mainDir, subfolderNames{i});
secondLevelFolders = dir(subDirPath);
isSubSub = [secondLevelFolders(:).isdir];
secondLevelNames = {secondLevelFolders(isSubSub).name}';
secondLevelNames(ismember(secondLevelNames, {'.', '..'})) = [];
% 遍历每个孙子文件夹
for j = 1:length(secondLevelNames)
finalFolderPath = fullfile(subDirPath, secondLevelNames{j});
files = dir(fullfile(finalFolderPath, '*.*')); % 列出所有文件
% 遍历并重命名每个文件
for k = 1:length(files)
if ~files(k).isdir % 确保它是一个文件而非目录
oldName = files(k).name;
[~, ~, ext] = fileparts(oldName); % 获取文件扩展名
newName = strcat(subfolderNames{i}, '_', secondLevelNames{j}, '_', num2str(k), ext); % 创建新文件名
oldFilePath = fullfile(finalFolderPath, oldName);
newFilePath = fullfile(finalFolderPath, newName);
movefile(oldFilePath, newFilePath); % 重命名文件
end
end
end
end
matlab: 文件夹重命名之以父文件加子文件的名称命名孙子文件
于 2024-05-12 15:38:26 首次发布