How to maintain variable in second concatenated for loop

Hi everyone,

I’m trying to get a “for” loop inside another “for” loop and don’t figure out what I’m doing wrong.

I have one parent folder “dir”, that is queried at the start, containing subfolders (“folder1”, “folder2”, “folder3”, etc). The purpose is to analyze individually each image in folder1, folder2, etc, and then save the resulting images to another folder (called “Normalized” in the macro below), in the same parent directory of the one queried at the start and maintaining the same structure (result of analysing “parent / dir / folder1” should go to “parent / Normalized / folder1”):


@File(style="directory", label="images to analyse") dir

list = getFileList(dir);
File.makeDirectory(File.getParent(dir) + File.separator + 'Normalized');
for (a=0; a<list.length; a++) {
	//print (list[a]);
	if (endsWith(list[a], "/"))  {
		image_diff = getFileList(list[a]);
		output_dir = File.getParent(dir) + File.separator + 'Normalized' +
		File.separator + list[a];
		File.makeDirectory(output_dir);
		list_a = list[a];
		//print(list_a);
		for (i=0; i<image_diff.length; i++)  {
			path1 = dir + File.separator + list[a] + image_diff[i];
	        print(list[a]);		
            print(list_a);
			print(path1);
			}
	}
}

using several “print()” inside the second loop I realize that the variable list[a] defined in the first “for” loop is not maintained inside the second “for” loop. None of the print statements containing list[a] as a variable work. list[a] is the name of the folder containing the images to analyze and is necessary to create a second directory to place the analysis result.

Thanks for your help!

After spending hours on this one, and finally giving up and deciding to write the post above, I found my mistake. I guess the exercise of writing it down has given me some clarity and I found out what I did wrong:

when generating the second list (image_diff) I used only the name of the folder (list[a]) instead of the entire path (dir + list[a]), the line corrected reads now

image_diff = getFileList(dir + File.separator + list[a]);

and the code from above becomes


@File(style="directory", label="images to analyse") dir

list = getFileList(dir);
File.makeDirectory(File.getParent(dir) + File.separator + 'Normalized');
for (a=0; a<list.length; a++) {
	//print (list[a]);
	if (endsWith(list[a], "/"))  {
		image_diff = getFileList(dir + File.separator + list[a]);  //here this made the difference
		output_dir = File.getParent(dir) + File.separator + 'Normalized' +
		File.separator + list[a];
		File.makeDirectory(output_dir);
		list_a = list[a];
		//print(list_a);
		for (i=0; i<image_diff.length; i++)  {
			path1 = dir + File.separator + list[a] + image_diff[i];
			print(list_a);
			print(path1);
			}
	}
}

Newbie mistakes I guess… but, many thanks even if you took the time to check this.