How can i copy a full folder into another one?

37 次查看(过去 30 天)
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 个评论
Jan
Jan 2021-10-30
编辑:Jan 2021-10-30
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)

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2021-10-30
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')

类别

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