ImageJ macro open 2 specific files in one folder

Hi, I’'m student and it’s the first time that works with ImageJ macro function.
I want to program with ImageJ.
I want to create a macro that can open 2 files in one folder automatically.And repeat for another folder.
This process needs to be automatically.
I really don’t find any code for this.

Can anyone help me?

Hi @Yasmine_student,

welcome to the forum.
Are the files you want to read images or other type of files?
Are there only 2 files in the folder or more and you need 2 specific ones?

Dependent on the answer to those questions the implementation might change.

Generally, it could be achieved as follows:

dir = getDir("select a directory");

fileList = getFileList(dir);

for (i = 0; i < fileList.length; i++) {
	open(dir + fileList[i]);
	//add more code here to do something with the images/files

If you copy-paste this code in the Script editor and run it as a macro, it will ask you for the folder and then open all images inside

Oh, thank you for you respons!

No in the folder are more than two files, it means that I need to filter the files.

How can I do that?

This depends on what kind of specification you want to filter them for (I assume some part of the file name). Could you specify a little more detailed how you finally would need to select them (e.g. with a screen shot or providing image names and parameter you need to look for)

I want in this folder only the first 2 files;



Maybe I can add, I have more folders that I want to open automatically.

I have 9 folders and in the folders are other folders with numbers, I want that Image J open it from up to down. Than I want in that folder to open 5_TSE_Dixon_2x2x6mm_lever_F.nii

In a simple way you would solve this as follows:

chosenDirectory = getDirectory("Choose your super folder ");

processFiles(chosenDirectory);	//necessary to run the function

//define the function
function processFiles(currentDir) {

	fileList = getFileList(currentDir);
	for(i=0; i<fileList.length; i++) {
		if(startsWith(fileList[i], "5_TSE") || startsWith(fileList[i], "6_TSE")) {
			//here goes your processing code
		} else if (endsWith(fileList[i], "/")) {
			processFiles(currentDir + fileList[i]);	

Wow, I can not thank you enough!
But he still don’t open 6_TSE
Can you help again?

Hm, that is weird. In my test it opened files no matter if they were labelled 5_TSE or 6_TSE in the beginning.
In the code, I don’t see a problem regarding the file names.

Do you get any error messages instead?
Then it might be linked to a problem with the files.

Otherwise, I honestly have no idea, what might be the problem without having the files at hand.

oh sorry, it works!
Thank you so much!!!

1 Like