Login  Register

Batch rename DICOM files with header info

Posted by Michael Doube-2 on Sep 26, 2007; 2:51pm
URL: http://imagej.273.s1.nabble.com/Batch-rename-DICOM-files-with-header-info-tp3698314.html

Hi All

I was hoping this would be a quick job but I am stuck.  I have a
directory full of DICOM images and matching bitmap thumbnails, both
named with a timestamp by the capture software.  I'd like to rename all
the DICOM files and matching BMP's with the string contained in header
0010,0010.  I can get the string, but don't seem to be able to rename
the files with it.  Any help appreciated,

Mike

macro:
------------------------------------
setBatchMode(true);
open();
path = File.directory();
tag = "0010,0010";
close();

function getTag(tag) {
      info = getImageInfo();
      index1 = indexOf(info, tag);
      if (index1==-1) return "";
      index1 = indexOf(info, ":", index1);
      if (index1==-1) return "";
      index2 = indexOf(info, "\n", index1);
      value = substring(info, index1+1, index2);
      return value;
  }

list = getFileList(path);
showStatus("Getting File List...");
for (i=0; i<list.length; i++){
    if(endsWith(list[i], '.dcm')){
        open(list[i]);
        newtitle = replace(getTag(tag),'^','_');
        newtitle = replace(newtitle, ' ', '_');
        newtitle = substring(newtitle, 1, lengthOf(newtitle)-1);
        print(newtitle);
        close();
        File.rename(list[i], path+newtitle+".dcm");
        thumbnail = replace(list[i], ".dcm", ".bmp");
        File.rename(thumbnail, path+newtitle+".bmp");
    }
}
----------------------------------