Help with macro for opening and then saving stacks of images

I have a folder with multiple sub-folders, each of which contains a number of DICOM (.dcm) files. I’d like to open all the DICOM files as a stack in imageJ, then save it say as a 3D TIFF. Also, I would like to do this using batch processing, since there are many different folders.

Looking at some online documentation, I came up with this macro, but it seems to generate really large file sizes (GB range) for some of the saved TIFFs, which makes me think it is iterating over multiple folders and getting all the DICOMS on multiple patients, rather than doing one patient/folder at a time.

Any help would be much appreciated!

Here is the code that I have currently:

// "BatchProcessFolders"
//
// This macro batch processes all the files in a folder and any
// subfolders in that folder. In this example, it runs the Subtract 
// Background command of TIFF files. For other kinds of processing,
// edit the processFile() function at the end of this macro.

   requires("1.33s"); 
   dir = getDirectory("Choose a Directory ");
   setBatchMode(true);
   count = 0;
   countFiles(dir);
   n = 0;
   processFiles(dir);
   //print(count+" files processed");
   
   function countFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              countFiles(""+dir+list[i]);
          else
              count++;
      }
  }

   function processFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              processFiles(""+dir+list[i]);
          else {
             showProgress(n++, count);
             path = dir+list[i];
             processFile(path);
          }
      }
  }

  function processFile(path) {
       if (endsWith(path, ".dcm")) {
           open(path);
           run("Image Sequence...", "open=path sort");
           saveAs("TIFF", path+"_full.tiff");
           close();
      }
  }

@tubbydonut

Hi! So I am a bit of a novice myself… but taking a quick look at your macro - I would remove all the code regarding counting files… I think that is what might be causing your issue, so try streamlining it a bit. Something like this:

// @File(label = "Input directory", style = "directory") input
// @File(label = "Output directory", style = "directory") output
// @String(label = "File suffix", value = ".tif") suffix

/*
 * Macro template to process multiple images in a folder
 */

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
// this way, you only process the files you need to...
function processFolder(input) {
	list = getFileList(input);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + list[i]))
			processFolder("" + input + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	// do YOUR SPECIFIC processing here by replacing
	// the following two lines by your own code
	// leave them in until things are working... then replace all with your own code
	print("Processing: " + input + file);
	print("Saving to: " + output);
}

Hope it helps a bit. But perhaps someone can give you a better, more specific answer regarding your own code.

thanks!

Still had some issues, but I wonder if the solution may have to do something with

“openImagesAsStack()” rather than open().

Anyways, I did it manually in the meantime.