Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Cedric
Cedric am 7 Okt. 2017
Bearbeitet: Cedric am 7 Okt. 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 Kommentare
Cedric
Cedric am 8 Okt. 2017
Awesome! I'll keep an eye on the thread in case there is anything.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by