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 件のコメント
per isakson
2014 年 8 月 28 日
編集済み: per isakson
2014 年 8 月 28 日
See the functions sprintf and datestr
回答 (1 件)
per isakson
2014 年 8 月 28 日
編集済み: per isakson
2014 年 8 月 30 日
"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
カテゴリ
ヘルプ センター および File Exchange で File Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!