필터 지우기
필터 지우기

how should I copy the selected files to another folder whose name contains 'mytext'

조회 수: 3 (최근 30일)
I have length(F) files are there in a folder. This program gives the 'fname' which contains all the selected files whose name contains '201306'. I want to move/copy these selected files to another folder. How should I do this from the script. Doing manually just by reading the names is such a difficult task and not satisfactory for myself.
F = dir('*.nc');
c = 0 ;
for j = 1:length(F)
fname = F(j).name ;
if ~isempty(strfind(fname,'201306'))
c = c+1 ;
i{c} = fname ;
end
end

채택된 답변

Cedric
Cedric 2017년 10월 7일
편집: Cedric 2017년 10월 7일
Use
copyfile( fname, dest ) ; % or movefile( fname, dest ) ;
where dest can be defined as
dest = fullfile( 'DestinationFolder', fname ) ; % or just a folder name
in your IF statement instead of the two lines that you have now.
MOST IMPORTANT: test using a copy of your files first! If you make a mistake when building the destination of MOVEFILE, you can easily loose files (by overwrite).
PS1: note that both functions accept wildcards, so you can certainly avoid the loop and leave it to the wildcard/pattern/function to copy/move all relevant files in one shot.
PS2: the destination can be a folder name, there is no need for a file name. Yet, if you want to rename files (e.g. prefix/suffix), you can do it by defining a destination file name.
  댓글 수: 6
Cedric
Cedric 2017년 10월 8일
Awesome! I'll keep an eye on the thread in case there is anything.

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

추가 답변 (0개)

카테고리

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