how should I copy the selected files to another folder whose name contains 'mytext'
3 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Cedric
on 7 Oct 2017
Edited: Cedric
on 7 Oct 2017
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 Comments
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!