Set input and output dir, then run. The program will search each folder in input dir, read DICOM info from first image of each stack and set age, name and gender as output folder name.
maindir = 'E:\C-Tsinghua\dataF\201607211307'; Destdir='E:\C-Tsinghua\dataF'; subdir = dir( maindir ); for i = 1 : length( subdir ) if( isequal( subdir( i ).name, '.' )||... isequal( subdir( i ).name, '..')||... ~subdir( i ).isdir) continue; end subdirpath= fullfile( maindir, subdir( i ).name); subdir2=dir( subdirpath ); subdir2path = fullfile( maindir, subdir( i ).name,subdir2(3).name,'*.dcm' ); dat = dir( subdir2path ) ; filename = fullfile( maindir, subdir( i ).name, subdir2(3).name,dat(3).name); metadata=dicominfo(filename); patientname=metadata.PatientName.FamilyName; sepind=find(patientname(:)==' '); sepend=size(patientname,2); if size(sepind,2)>1 sepend=sepind(2); sepind=sepind(1); end patientnamen=[patientname(1) lower(patientname(2:sepind-1)) patientname(sepind+1) lower(patientname(sepind+2:sepend))]; patientsex=metadata.PatientSex; patientage=str2num(metadata.ContentDate(1:4))-str2num(metadata.PatientBirthDate(1:4)); newfoldername=[mat2str(patientage), '_', patientnamen, '_' , patientsex]; movfrom=fullfile( maindir, subdir( i ).name,subdir2(3).name); movto=fullfile(Destdir,newfoldername); movefile(movfrom,movto); end