Ij1: Applying a function to an element in an array (in a loop)

So, first of all I’m a very beginner in this so I don’t know if what I am trying to do makes any sense at all but here it goes:

dir is an array consisting of the names of the subfolders in my main directory.
I am trying to create a function (ListImages) which loops through all files in each subfolder (one by one), to find an image with a specific name (FileName) (should be only one per folder), which will be later further analysed (processImage function) - other images should be just ignored.
So I guess I need a nested loop for the ListImages function, but it doesn’t seem to work.

Upon further inspection, I realised that I can’t seem to get getFileList to work for individual elements of an array at all, e.g. files = getFileList(dir[0]); followed by Array.print(files) doesn’t give me anything, so I guess me entire strategy is wrong.

Is there any other way to loop through elements of an array? Or am I missing something bigger than here?

function ListImages(dir[m]) {
	for (m=0; m< dir.length; m++) { 
		AllFiles = getFileList(dir[m]);
	for (i = 0; i < AllFiles.length; i++) {
		if (startsWith(FileName)) {


If I were you - I would use the available templates for processing files in folders/subfolders. Open the script editor, select Templates :arrow_forward: ImageJ 1.x :arrow_forward: Batch :arrow_forward: Process Folder (IJ1 Macro) . Here is the code:

 * 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.


// 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)) {
                  // here you can add another if statement to check the file 
                  // name... and only process if it matches
			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);

1 Like

Thanks @etadobson, this is the template I used as a basis for my own functions, later I will try to remember why I had to over-complicate it :smiley: :smiley: (or if it was necessary at all)


No worries! If you have any issues… just post a minimal working example of your code here. We are always here to help!

1 Like

ok now I realised why I have to over-complicate it
The reason I am introducing this whole “dir” list and looping system is because I am using this to incorporate in a very long existing macro. Essentially I want to modify this long macro to loop through all subdirectories and find an image with a specific name to analyse.
In this (existing) macro, the folder where the images are processed is called “dir” throughout (and has huge amounts of connections to it in various ways), so I am trying to make this code loop through all directories by making into dir[m] everywhere.

Apologies for convoluted explanations!
I think I will adopt a new strategy and try to simplify this :slight_smile:
But I appreciate any ideas about the original question anyway, thanks!

1 Like