create subfolders that are the same as those in another folder

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

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?

Sign in to comment.

 Accepted Answer

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

I thank you for your reply, but it reports me this error:
Brace indexing is not supported for variables of this type.
in questa riga:
mkdir(fullfile(newPath, dfolders{k}));
@Alberto Acri: I've confused dfolders{k} with dfolders(k).name . Of course you can use the debugger to fix such problems by your own.
thank you, but it still doesn't work :)
@Alberto Acri: It would be useful to solve your problemn, if you mention, what "doesn't work" means. I cannot guess this detail.
Thank you. It works. Can you tell me how to extract a column present in the struct?
Which column of which struct do you want to extract? "Structs" do not have "columns".

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Asked:

on 8 Nov 2022

Commented:

Jan
on 10 Nov 2022

Community Treasure Hunt

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

Start Hunting!