moving folders main folder and then deleting the empty folders

5 ビュー (過去 30 日間)
Amanda
Amanda 2021 年 2 月 8 日
回答済み: Sourabh Kondapaka 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...,
except, i have hundreds of people
I want to put these SE1 folders into just "data" and remove the original folders they were held inside
how can I make such a script?
thank you!!
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 8 日
To confirm, you would throw away person/SE3 and person/SE8 ? Throw away everything except person/SE1 ?
I take it that "person" for this purpose is variable, such as data/Amanda/SE1 data/Amanda/SE3 data/THX1138/SE1 data/BB8/SE1 ? But SE1 is a fixed name, right? When you do the moving, what should the new name be? data/Amanda/SE1 should be renamed to... what?
Amanda
Amanda 2021 年 2 月 8 日
main folder= "data"
subfolders of "data"= "Person 1" "Person2" "Person3" etc....
subfolder of "Person1" = SE1, SE2
subfolder of "Person2" = SE3, SE8
I want SE1, SE2, SE3, SE8, etc to be removed from the folders that they are in
and put into "data" folder with the original PersonNumber folders they were in.
The deleting part is extra, I just want these files to be moved.
But I would delete Person1 once that folder has its contents emptied/moved
thanks!!

サインインしてコメントする。

回答 (1 件)

Sourabh Kondapaka
Sourabh Kondapaka 2021 年 2 月 11 日
Approaching this in a similar fashion to your question answered here
Code:
pathToData = fullfile(pwd, 'data');
dataFolder = dir(pathToData);
dirFlags = [dataFolder.isdir];
subFolders = dataFolder(dirFlags);
subFolders = subFolders(3:length(subFolders));
for k = 1 : length(subFolders)
person = subFolders(k).name;
personSubFolder = dir(fullfile(pathToData, person));
personSubFolder = personSubFolder(3:length(personSubFolder));
for j = 1: length(personSubFolder)
seFolder = personSubFolder(j).name;
srcFolder = fullfile(pathToData, person, seFolder);
destFolder = fullfile(pathToData, seFolder);
movefile(srcFolder, destFolder);
% fprintf('\n \n ------------- Moving from ---------- \n \n');
% disp(srcFolder);
% fprintf('\n \n To \n \n');
% disp(destFolder);
end
%Removing the person folder after moving all the folders within it into
%Data folder
rmdir(fullfile(pathToData, person));
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

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by