Renaming folders within folders to the name of original folder
조회 수: 2(최근 30일)
I have one big folder, lets call "data", which has folders inside called 'person'.
Each "person" has specific folders, sometimes "SE1" and "SE3", sometimes SE1 and SE8, etc..., it's random.
I want these this SE1, SE2, folders, etc, to be renamed to "person1 SE1" , "person 1 SE3" "Person2 SE1" "Person2 SE9" etc...
essentially to be the name of the folder it's in, but keeping it's current foldername.
Can someone help formulate a script for me? I'm very new to Matlab.
Sourabh Kondapaka 2021년 2월 11일
편집: Sourabh Kondapaka 2021년 2월 11일
Check out the below code
% This is the path to data folder
pathToData = fullfile(pwd, 'data');
dataFolder = dir(pathToData);
dirFlags = [dataFolder.isdir];
subFolders = dataFolder(dirFlags);
%Considering values from 3rd index to not include '.' and '..'
subFolders = subFolders(3:length(subFolders));
for k = 1 : length(subFolders)
% Get the name of the person folder within data folder
person = subFolders(k).name;
personSubFolder = dir(fullfile(pathToData, person));
% Get subFolders within the person folder. Excluding '.' and '..'
personSubFolder = personSubFolder(3:length(personSubFolder));
for j = 1: length(personSubFolder)
seFolder = personSubFolder(j).name;
% srcFolder is the original folder. destFolder is the new Name of the folder
% I'm using the movefile() function to rename accordingly
srcFolder = fullfile(pathToData, person, seFolder);
destFolder = fullfile(pathToData, person, person + " " + seFolder);
% fprintf('\n \n ------------- Renaming from ---------- \n \n');
% fprintf('\n \n To \n \n');
Initial Folder Structure:
After executing the above script:
For more information on "movefile()" you can check the documention here
For more information on "dir()" you can check the documentation here
A similar question has been answered here
You can take the free course Matlab OnRamp