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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Cedric
Cedric el 7 de Oct. de 2017
Editada: Cedric el 7 de Oct. de 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 comentarios
Cedric
Cedric el 8 de Oct. de 2017
Awesome! I'll keep an eye on the thread in case there is anything.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by