how to rename or move files
Afficher commentaires plus anciens
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 commentaire
per isakson
le 28 Août 2014
Modifié(e) : per isakson
le 28 Août 2014
See the functions sprintf and datestr
Réponses (1)
per isakson
le 28 Août 2014
Modifié(e) : per isakson
le 30 Août 2014
"from one name to another in the same folder."   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.
 
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
Catégories
En savoir plus sur Large Files and Big Data dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!