按照txt中指定的文件名,从src_path中拷贝文件到dest_path(copyfile_from_txt)

来源:互联网 发布:mac如何安装ipython 编辑:程序博客网 时间:2024/04/30 09:08
function num_processed = copyfile_from_txt(txt_filename,src_path,dest_path,add_num,ext_name)% Author:shizhixin% Email:szhixin@gmail.com% Blog:http://blog.csdn.net/shizhixin% Date:2012-03-02% Function:按照txt中指定的文件名,从src_path中拷贝文件到dest_path文件夹下% Note: txt文件格式如下,% no obj_fitsname% 1 spec-SSY02_1_sp07-156% 从第二行start_row=2开始,第二列为文件名(find space position)% Example:函数可以拷贝如spec-SSY02_1_sp07-156-[1...n].png的文件% 若src_path中有如下文件:% spec-SSY02_1_sp07-156-1.png% spec-SSY02_1_sp07-156-2.png% spec-SSY02_1_sp07-156.fits%% add_num = 0;% ext_name = '.fits'% 则拷贝spec-SSY02_1_sp07-156.fits文件%% add_num = 2;% ext_name = '.png'% 则拷贝spec-SSY02_1_sp07-156-1.png和spec-SSY02_1_sp07-156-2.png文件% add_num = 2;% ext_name = '.png'% txt_filename = 'H:\20120301_peakfit_result\20120301_ssy_two_lines_10122\obj_filename.txt';% src_path = 'H:\20120301_peakfit_result\20120301_ssy_two_lines_10122\dbpeakfit_png\';% dest_path = 'test2\';%copyfile_from_txt(txt_filename,src_path,dest_path,add_num,ext_name)start_row = 2;num_processed = 0;[file_info] = importdata(txt_filename);len = length(file_info)for i=start_row:len    try        str = file_info{i};        pos = find(isspace(str)==1);%找到空格位置,即第二列的文件名        filename = str(pos+1:end);        filename = strtrim(filename);        if add_num==0            copy_name = [filename ext_name];            copyfile([src_path copy_name], [dest_path copy_name]);        else            for j=1:add_num                copy_name = [filename '-' num2str(j) ext_name];                copyfile([src_path copy_name], [dest_path copy_name]);            end        end%if        num_processed = num_processed+1;    catch        disp(lasterr)    end %try ... catchend %forend%function

运行结果:

add_num = 2;
 ext_name = '.png'
 txt_filename = 'H:\obj_filename.txt';
 src_path = 'H:\dbpeakfit_png\';
 dest_path = 'test\';
copyfile_from_txt(txt_filename,src_path,dest_path,add_num,ext_name)

ext_name =

.png

len =

   234
%文件名不存在会报错,但是不会终止程序运行结束
Error using ==> copyfile
No matching files were found.
Error using ==> copyfile
No matching files were found.

ans =

   231

原创粉丝点击