Renaming folders within folders to the name of original folder

조회 수: 2 (최근 30일)
Amanda
Amanda 2021년 2월 8일
편집: Stephen23 2021년 2월 11일
Hello,
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.
thank you!!

답변 (1개)

Sourabh Kondapaka
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);
movefile(srcFolder, destFolder);
% fprintf('\n \n ------------- Renaming from ---------- \n \n');
% disp(srcFolder);
% fprintf('\n \n To \n \n');
% disp(destFolder);
end
end
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
Note: As your query was simple enough, a for-loop approach was sufficient. If you need to achieve something much more complicated than this, please read up about Recursion, Backtracking
  댓글 수: 1
Stephen23
Stephen23 2021년 2월 11일
편집: Stephen23 2021년 2월 11일
A very good answer, but let down by this one piece of poor advice:
%Considering values from 3rd index to not include '.' and '..'
subFolders = subFolders(3:length(subFolders)); % FRAGILE AND MISLEADING
Much better advice:
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by