How to copy a file to multiple folders using the command line?

I have tried to copy a file test.txt to multiple directories with one command:
for ii=0:5:175
for jj=0:3:90
newdir=sprintf('phi=%2dtheta=%2d',ii,jj);
mkdir(fullfile(newdir))
end
end
for ii=0:5:175
for jj=0:3:90
source_dir=sprintf('G:/SYSTEM MATRIX - MCNP/phi%2dtheta%2d',ii,jj);
copyfile('test.txt','source_dir')
end
end
But I didn't succeed. Is there a way to do that in one command so I can copy a file or even a folder to multiple directories?

 채택된 답변

Image Analyst
Image Analyst 2016년 12월 30일

0 개 추천

What does "didn't succeed" mean? Did it finish with no errors but there were no files there? Or did it give an error message? copyfile() takes the destination folder as the second argument, not the source folder. As if that wasn't bad enough, you're passing the string 'sourcedir' instead of sourcedir so it's not even passing the string you want it to. To fix, get rid of the single quotes. And make sure all folders exist before you call copy file. exist(folder, 'dir') is useful for that purpose.

댓글 수: 4

I do not make sense.
I know you don't. That's why I asked you questions -- to try to make sense of what you said.
You might like to read the FAQ related to your question: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
Are you still having the problem?
thanks. but I think it is different my problem. I have only one file test.txt and I have to copy and paste this file to 1000 folders different.
Create the destination folder in a loop
baseFileName = 'whatever.dat';
inputFullFileName = fullfile(inputFolder, baseFileName);
for k = 1 : 1000
outputFolder = sprintf('...........whatever
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
outputFullFileName = fullfile(outputFolder, baseFileName);
copyfile(inputFullFileName, outputFullFileName);
end

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

질문:

2016년 12월 30일

댓글:

2017년 1월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by