필터 지우기
필터 지우기

Copy file to a new folder

조회 수: 20 (최근 30일)
Jason
Jason 2016년 3월 7일
답변: Prajwol Tamrakar 2020년 5월 27일
Under a folder, I have a lot of .mat. "19800101.mat", "19800102.mat", ......."19800201.mat", "19800202.mat", ......"19801201.mat".... I want to copy "19800101.mat", "19800102.mat", and copy others which also belong to January to a new folder, and display this new folder directory. February to a another new folder, and so on. Thanks.

채택된 답변

Walter Roberson
Walter Roberson 2016년 3월 8일
destdirs = {'January', 'February', 'March', ... 'December'};
ndests = length(destdirs);
for K = 1 : ndests
if ~exist(destdirs{K}, 'dir'); mkdir(destdirs{K}); end
end
dinfo = dir('*.mat');
filenames = {dinfo.name};
%remove filenames that are not a 4 digit year followed by a 2 digit month followed by a
%2 digit day followed by .mat. Years starting in 0, 1, or 2 are accepted with this check
namebad = cellfun(@isempty, regexp(filenames, '[012]\d\d\d[01]\d\d\d\.mat$') );
filenames(namebad) = [];
nfiles = length(filenames);
for K = 1 : nfiles
thisfile = filenames{K};
monthnum = sscanf(thisfile, '%*4d%2d', 1);
if monthnum > ndests
fprintf('File has invalid month number, skipping "%s"\n', thisfile);
else
destdir = destdirs{monthnum};
copyfile( thisfile, destdir );
end
end
  댓글 수: 4
Jason
Jason 2016년 3월 8일
Thank you so so so much. It works for me now.
Walter Roberson
Walter Roberson 2016년 3월 8일
Please click to Accept the answer.

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

추가 답변 (2개)

John BG
John BG 2016년 3월 8일
편집: John BG 2016년 3월 8일
Hi Jason
one way to copy files is with command dos
path1='put here full destination path' % for instance D:\Documents\backup\'
dos('copy file_name.mat path1')
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John
  댓글 수: 2
Jason
Jason 2016년 3월 8일
Thanks. But I do not think your code cannot work.
Jan
Jan 2016년 3월 8일
MAtlab's copyfile is more direct and faster than starting a DOS command. If there is really a reason to call DOS, use:
dos(['copy file_name.mat ', path1])

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


Prajwol Tamrakar
Prajwol Tamrakar 2020년 5월 27일
This issue of copying file to another directory is possibly due to "ready only access". Use 'f' option to override the read-only status of the destination folder.
copyfile(SourceFile, DestinyFile, 'f')

카테고리

Help CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by