//---------------------------------------------- // Reorder a directory with a lot of dicom // files by moving the files in sub directories // sorted by patient id, study id etc. // // Alberto Duina // Spedali Civili di Brescia // Servizio di Fisica Sanitaria // Brescia, Italy // 27 feb 2011 //---------------------------------------------- requires("1.45c"); dir1 = getDirectory("Choose Source Directory "); setBatchMode(true); count = 0; countFiles(dir1); n=0; processFolder(dir1); print (count+ " files processed"); // // called for every image file // function processFile(path0, path1) { setBatchMode(true); IJ.redirectErrorMessages(); open(path0); if (nImages >=1) { name1 = File.getName(path0); dir2 = File.getParent(path0); info1=getImageInfo(); coil1 = trim(getInfo("0051,100F")); seqNum = trim(getInfo("0018,1030")); serieNum = trim(getInfo("0020,0011")); patientName = trim(getInfo("0010,0010")); sequenceName = trim(getInfo("0018,0024")); Acquisition = trim(getInfo("0020,0012")); studyID = trim( getInfo("0020,0010")); // creation of new directory tree directoryPatientName = dir1+patientName; if (!File.exists(directoryPatientName)) File.makeDirectory(directoryPatientName); directoryStudyName = directoryPatientName+"/Study_"+studyID; if (!File.exists(directoryStudyName)) File.makeDirectory(directoryStudyName); directorySeriesName = directoryStudyName+"/Series_"+serieNum; if (!File.exists(directorySeriesName)) File.makeDirectory(directorySeriesName); // move the file in the new path path3=directorySeriesName+"/"+name1; a=File.rename(path0, path3); } } // // copied from BatchProcessFolders // function countFiles(dir) { list = getFileList(dir); for (i=0; i