필터 지우기
필터 지우기

How can I copy a folder into another folder?

조회 수: 147 (최근 30일)
Kian Azami
Kian Azami 2017년 9월 26일
댓글: Kian Azami 2017년 9월 27일
In my current folder I have created two empty folders, one is "Folder1" and the other is "Folder2" and I want to copy one of these folders to another one by writing code in a matlab script. I used both of these commands:
copyfile('Folder1','Folder2')
copyfile('Folder1','C:\Users\st1azamiki\Desktop\new\Folder2')
But these commands do not copy the Folder1 inside the Folder2. Do not produce an error also. What am I doing wrong? How can I copy a Folder from a particular path to another particular path?
  댓글 수: 2
Jan
Jan 2017년 9월 26일
Please post the orignial command. "does not copy the Folder1 inside the Folder2" does not allow to know, what happens instead. Do you get an error message or is the folder copied somewhere else? Without the code and a description what happens, it is hard to suggest an improvement.
Kian Azami
Kian Azami 2017년 9월 27일
Hello Jan, Now I have edited my comment and I put the commands that I used and I think it is understandable now. I do not get an error message also, the commands seem to work but it does not copy the Folder1 into Folder2. I think I make a small mistake in doing these commands but I don't know yet.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2017년 9월 27일
편집: Jan 2017년 9월 27일
copyfile('Folder1', 'Folder2') copies the contents of Folder1 into Folder2:
cd(tempdir);
mkdir('Folder1')
mkdir('Folder2')
fid = fopen('Folder1\test.txt', 'w');
fclose(fid);
copyfile('Folder1', 'Folder2')
dir('Folder2')
Now Folder2 contains a copy of test.txt. But you want Folder2 to contain an instance of Folder1. Then do this explicitly:
copyfile('Folder1', 'Folder2\Folder1')
Now the contents of the (empty or non-empty) Folder1 is copied to Folder2\Folder1.
I used relative path names here for compactness. The problem of your command was not the absolute path, but the missing of the name of the destination folder. In productive code a GUI or timer callback can change the current folder, therefore I use absolute path names in every case:
Folder1 = fullfile(tempdir, 'Folder1');
Folder1 = fullfile(tempdir, 'Folder2');
copyfile(Folder1, fullfile(Folder2, 'Folder1'));
  댓글 수: 1
Kian Azami
Kian Azami 2017년 9월 27일
Very useful answer Jan, it works properly.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KL
KL 2017년 9월 26일
편집: KL 2017년 9월 26일
copyfile 'folder_path/folder_1' 'folder_path/folder_2'
or
copyfile('folder_path/folder_1','folder_path/folder_2')
This simply should work. Get an output of the command and see if you get a logical 1 as answer.
  댓글 수: 1
Kian Azami
Kian Azami 2017년 9월 27일
The problem is that it doesn't work. If you can try it by yourself and see the result. It does not take more than 5 minutes. Try to copy an empty folder to another empty folder. And the logical output is 1.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by