How can i copy a full folder into another one?

52 views (last 30 days)
Hello everyone, I am currently struggling with the following problem:
I created a new folder with
mkdir newDir;
Then I considered a single folder taken from
folder = uigetdir ('my_path'...
,'select the folder to analyze');
Now, what I want to do is to have folder into newDir, but writing
copyfile (folder ,'newDir');
I have all the elements of folder copied into newDir, while I need the full folder structure to be preseved, since I am gonna add more folders later and I want to keep them separate in newDir.
What should I do?
Thanks for your help!
  1 Comment
Jan
Jan on 30 Oct 2021
Edited: Jan on 30 Oct 2021
The question is not clear yet: What do you want to copy to where? copyfile(A,B) stores the contents of the folder A in the folder B. All subfolders are copied also. So what is the difference to the needed preserving of the folder structure?
The actual question, hwo to copy a full folder to another folder is solved by your code already:
copyfile(A, B)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 30 Oct 2021
Nope, copyfile works fine to preserve the folder structure.
tn1 = tempname();
f1n = fullfile(tn1, 'f1');
f2n = fullfile(tn1, 'f2');
mkdir(tn1); mkdir(f1n); mkdir(f2n);
outn = fullfile(tn1, 'only_in_outer.txt')
outn = '/tmp/tpb344ea5c_31ad_4bfd_a2a9_34975aee51c9/only_in_outer.txt'
in1 = fullfile(f1n, 'only_in_f1.txt');
in2 = fullfile(f2n, 'only_in_f2.txt');
fclose(fopen(outn, 'w'));
fclose(fopen(in1, 'w'));
fclose(fopen(in2, 'w'));
ls(tn1), ls(f1n), ls(f2n)
f1 f2 only_in_outer.txt only_in_f1.txt only_in_f2.txt
tn2 = tempname();
mkdir(tn2);
copyfile(tn1, tn2)
ls(tn2), ls(fullfile(tn2, 'f1')), ls(fullfile(tn2, 'f2'))
f1 f2 only_in_outer.txt only_in_f1.txt only_in_f2.txt
rmdir(tn1, 's'); rmdir(tn2, 's')

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!