how to rename or move files
Show older comments
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 Comment
per isakson
on 28 Aug 2014
Edited: per isakson
on 28 Aug 2014
See the functions sprintf and datestr
Answers (1)
per isakson
on 28 Aug 2014
Edited: per isakson
on 30 Aug 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
Categories
Find more on App Building 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!