matlab 파일 이름 바 꾸 기 movefile

function rename_dir(path, option)
%Author:shizhixin
%Email:[email protected]
%Blog:http://blog.csdn.net/shizhixin
%Date:2012-02-15

%option = 'LAMOST','DR8','SPECIAL'
%LAMOST:  spec_release.php@path=%2Fdata1%2Fspectra_v2.3.3%2F20111024%
% 2FF5902%2Fspec-55859-F5902_sp02-166.fits    ,      
%     spec-55859-F5902_sp02-166.fits
%DR8: spec-pppp-mmmmm-ffff.fits
%fits@plateid=278&mjd=51900&fiber=583       spec-0278-51900-0583.fit
%        
%SPECIAL :Spec-2951-54592-433.fit  ---> spec-pppp-mmmmm-ffff.fits

%EX:rename_dir('G:\DR8_data\', 'DR8')
switch option
    case 'LAMOST'
        files = dir([path '*.fits']); %     .fits     
        n = length(files);           %    
        for i = 1:n
            i
            filename = [path files(i).name];    %   ,    
            old_name = files(i).name;           %       
            pos = strfind(old_name, 'spec-');   %  spec-   
            new_name = old_name(pos:end);       % spec-                 
            old_path = strcat(path,old_name);   %          
            new_path = strcat(path,new_name);   %         
            status = movefile(old_path,new_path); %      ,     
        end
    case 'DR8'
        files = dir([path '*']); %      
        n = length(files);           %    
        for i = 1:n
            i
            filename = [path files(i).name];    %   ,    
            old_name = files(i).name;           %       
            pos_equal = strfind(old_name, '=');   %  =    
            pos_and = strfind(old_name, '&');   %  &   
            if length(pos_equal)~=3
                continue;
            end
            pppp = old_name(pos_equal(1)+1:pos_and(1)-1);
            pppp = alignbitzero(pppp ,4);
            mmmmm = old_name(pos_equal(2)+1:pos_and(2)-1);
            mmmmm = alignbitzero(mmmmm ,5);
            ffff = old_name(pos_equal(3)+1:end);
            ffff = alignbitzero(ffff ,4);

            new_name = ['spec-' pppp '-' mmmmm '-' ffff '.fits'];
            % spec-                 
            old_path = strcat(path,old_name);   %          
            new_path = strcat(path,new_name);   %         
            status = movefile(old_path,new_path); %      ,     
        end
    case 'SPECIAL'
        %Spec-2951-54592-433.fit  ---> spec-pppp-mmmmm-ffff.fits
        files = dir([path '*.fit']); %      
        n = length(files);           %    
        for i = 1:n
            i
            filename = [path files(i).name];    %   ,    
            old_name = files(i).name;           %       
            pos_equal = strfind(old_name, '-');   %  =    
            pos_point = strfind(old_name, '.');   %  .    
            if length(pos_equal)~=3
                continue;
            end
            pppp = old_name(pos_equal(1)+1:pos_equal(2)-1);
            pppp = alignbitzero(pppp ,4);
            mmmmm = old_name(pos_equal(2)+1:pos_equal(3)-1);
            mmmmm = alignbitzero(mmmmm ,5);
            ffff = old_name(pos_equal(3)+1:pos_point-1);
            ffff = alignbitzero(ffff ,4);

            new_name = ['spec-' pppp '-' mmmmm '-' ffff '.fits'];
            % spec-                 
            old_path = strcat(path,old_name);   %          
            new_path = strcat(path,new_name);   %         
            status = movefile(old_path,new_path); %      ,     
        end

    otherwise
        disp 'option error!'
end
end

%old_num       bit ,   , alignbitzero(234,4)     0234
function new_num = alignbitzero(old_num, bit)
len = length(old_num);
if len > bit
    disp 'error';
end
for i=1:bit-len
    old_num=strcat('0', old_num);
end
new_num = old_num;
end

좋은 웹페이지 즐겨찾기