Batch processing multiple directories

I am trying to batch process multiple directories. I am able to batch process images within a directory, but I have been unable to figure out how to iterate over a list of directories. I have found multiple examples of how to iterate over nested folders, i.e. directories and subfolders, but I don’t know how to nest loop to iterate over directories that are not nested. I would like to supply a list of folders to then batch process each one. Here is my code, is it possible to add a nested for loop or something? I’m guessing it’s trivial, and I’m inexperienced.

setBatchMode(true);
dir = getDirectory("Select the folder where raw images are saved");
list = getFileList(dir);
output=dir + "segmented" + File.separator;
File.makeDirectory(output);

function processBatch(dir, filename){
			
	open(dir + filename);
	name = File.nameWithoutExtension; 
//stuff to do goes here
	saveAs("Tiff", output + name + "Mask.tif");

}

// Iterate through the file list

     for (i=0; i<list.length; i++) {
     	
     processBatch(dir, list[i]); 	
  		
     }

setBatchMode(false);

I would just use the macro batch example that is provided in Fiji. That should do the trick:
Macro editor > Templates > Image J 1.x > Batch > Process Folder (IJ1 Macro)

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

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

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	// Do the processing here by adding your own code.
	// Leave the print statements until things work, then remove them.
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
}

Piecing out what does what is a nice programming exercise as well =)

Thank you. I have looked into that example before, but this is where I am stuck. I understand how it scans through folders looking for files within nested folders. The problem that I have been unable to understand is how to process folders that are not nested. Ones that exist in different folders. I haven’t been able to figure out how to pass a list of folders into a the process folder function. Is this possible?

Well you need to give it only the directory/folder where your parallel folders are in.
This will pass a list of files or folders - for the getFileList() function, files or folders are the same basically, the latter I think would contain a “/” or “\” at the end - to the if statements.

You could even modify the first if statement to only include certain folders by expanding the condition.

Alternatively you could just make a text file I guess or your own array with locations and/or folder names and pass that instead of getFileList(input). Look at the array functions included in the macro language: https://imagej.nih.gov/ij/developer/macro/functions.html

Use the Array.show(array) function to review the content of your list variable or any array.