how to rename or move files

Hello,
I want to rename or move files from one name to another in the same folder. i write following script. but it gives errors.
clear all;
close all;
year=1998;
mn=1;
for dd=1:9
display(['dt_ref_med_merged_madt_h_',num2str(year),'0',num2str(mn),'0',num2str(dd),'.nc']);
movefile (['dt_ref_med_merged_madt_h_',num2str(year),'0',num2str(mn),'0',num2str(dd),'.nc' 'dt_ref_med_merged_madt_h_',num2str(year),'_',num2str(dd),'.nc']);
end
>>mytest
dt_ref_med_merged_madt_h_19980101.nc
??? Error using ==> movefile
Can not copy or move a file or directory onto itself.
Error in ==> mytest at 10
movefile (['dt_ref_med_merged_madt_h_',num2str(year),'0',num2str(mn),'0',num2str(dd),'.nc' 'dt_ref_med_merged_madt_h_',num2str(year),'_',num2str(dd),'.nc']);
how can I fix it ?
thanks.

1 comentario

per isakson
per isakson el 28 de Ag. de 2014
Editada: per isakson el 28 de Ag. de 2014
See the functions sprintf and datestr

Iniciar sesión para comentar.

Respuestas (1)

per isakson
per isakson el 28 de Ag. de 2014
Editada: per isakson el 30 de Ag. de 2014

0 votos

"from one name to another in the same folder." &nbsp I cannot see that you provide "another name"
movefile (['dt_ref_med_merged_madt_h_',num2str(year),'0',num2str(mn),'0',num2str(dd),'.nc']...
, ['dt_ref_med_merged_madt_h_',num2str(year),'_',num2str(dd),'.nc']);
"|],[|" in the middle (to split the unreadable code into two names) was missing.
&nbsp
EDIT: I find this easier to read
y = 2014;
m = 1;
d = 23;
sprintf( 'dt_ref_med_merged_madt_h_%s.nc' ...
, datestr([y,m,d,0,0,0],'yyyymmdd') )
ans =
dt_ref_med_merged_madt_h_20140123.nc

Categorías

Más información sobre Large Files and Big Data en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 28 de Ag. de 2014

Editada:

el 30 de Ag. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by