create subfolders that are the same as those in another folder
9 views (last 30 days)
Show older comments
I have 2 folders, "GLOBAL" and "GLOBAL2" on the Desktop.
Inside the folder "GLOBAL" I have folders "DATA 1", "DATA 4", "DATA 5", etc.
I would like to create, using a for loop, folders inside "GLOBAL2" that have the same name as the folders inside "GLOBAL" (i.e. create inside "GLOBAL2" the folders "DATA 1", "DATA 4", "DATA 5", etc.).
I haven't found any clues on the Internet or maybe I'm looking wrong....
% get the folder contents
d = dir('C:\Users\Alberto\Desktop\GLOBAL');
for k = ????
% remove all files (isdir property is 0)
dfolders = d([d(:).isdir]);
% remove '.' and '..'
dfolders = dfolders(~ismember({dfolders(:).name},{'.','..'}));
dfolders_1 = dfolders.name;
% subfolder creation
subfolder = fullfile(d, sprintf(dfolders_1));
if ~exist(subfolder, 'dir')
mkdir(subfolder);
end
EDIT:
I am reading .jpg images within different folders:
Now reading C:\Users\Alberto\Desktop\GLOBAL\DATA 4\example\image_1.jpg
Now reading C:\Users\Alberto\Desktop\GLOBAL\DATA 4\example\image_2.jpg
Now reading C:\Users\Alberto\Desktop\GLOBAL\DATA 4\example\image_3.jpg
...
Now reading C:\Users\Alberto\Desktop\GLOBAL\DATA 6\example\image_1.jpg
Now reading C:\Users\Alberto\Desktop\GLOBAL\DATA 6\example\image_2.jpg
Now reading C:\Users\Alberto\Desktop\GLOBAL\DATA 6\example\image_3.jpg
...
I want to create the folders "DATA 4" and "DATA6" automatically inside another folder called "GLOBAL2":
folder_new = 'C:\Users\Alberto\Desktop\GLOBAL2';
Let me give a clearer example:
Now reading:
Now reading C:\Users\Alberto\Desktop\GLOBAL\DATA 4\example\image_1.jpg
I want to create a folder with the name "DATA 4" inside "GLOBAL2":
subF = 'C:\Users\Alberto\Desktop\GLOBAL2\DATA 4';
As soon as it is read:
Now reading C:\Users\Alberto\Desktop\GLOBAL\DATA 6\example\image_1.jpg
I want to create a folder with the name "DATA 6" inside "GLOBAL2":
subF = 'C:\Users\Alberto\Desktop\GLOBAL2\DATA 6';
I hope this is clear :)
2 Comments
Jan
on 8 Nov 2022
I have spent some time to create a running version under your former question. I could not submit it, because you have deleted the question. Now this new question appears. Should I start to write an answer again?
Accepted Answer
Jan
on 8 Nov 2022
Edited: Jan
on 9 Nov 2022
oldPath = 'C:\Users\Alberto\Desktop\GLOBAL';
newPath = 'C:\Users\Alberto\Desktop\GLOBAL2';
d = dir(oldPath);
dfolders = d([d(:).isdir]);
dfolders = dfolders(~ismember({dfolders(:).name}, {'.','..'}));
for k = 1:numel(dfolders)
mkdir(fullfile(newPath, dfolders(k).name)); % [EDITED] Typo fixed
end
6 Comments
Jan
on 10 Nov 2022
Which column of which struct do you want to extract? "Structs" do not have "columns".
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!