Issues opening a file with variable name

Hello everybody,

I’m writing a macro to open a file with variable name.
First, i read a barcode to get the correct file name,
then i try to open the file.

open("C:/PROGRAMMEFINAL/BARCODE.jpg");	//open the barcode

run("Barcode Codec");	//a text window appears with the name of the right file, which is contained in the barcode

filename = getInfo("window.contents");	//get the name of the file i want to open

directory = "C:/PROGRAMMEFINAL/";	//get the directory where all my files are contained

tif = ".tif";	//add the extension

open(directory+filename+tif);	//open the right file

Unfortunately, an error message appears :

File is not in a supported format, a reader plugin is not available, or it was not found.

When I print(directory+filename+tif);
I get exactly the path of the file.

I’m new to imagej and i did’nt find a solution on ImageJ help,
Any help and insight is appreciated,

Thank you,


So… I think there might be an easier way to open and work with your files. Have you looked at Script Parameters at all? They will definitely simplify your life…

For example (there are multiple and perhaps simpler ways of doing this):

#@File(label = "Input directory", style = "directory") input
#@File(label = "Barcode file") barcode
#@String(label = "File suffix", value = ".tif") suffix

open(input + File.separator + barcode);
run("Barcode Codec");
filename = getInfo("window.contents");

// function to scan folders/subfolders/files to find files with correct suffix & filename
function processFolder(input) {
	list = getFileList(input);
	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) && startsWith(list[i], filename))
			processFile(input, list[i]);

function processFile(input, file) {
        open(input + File.separator + file);  // I'm not sure if this is the right call... you'll have to test this... can use print() statements to test as you did before...
// do other stuff as you need...

NOTE: I have NOT tested this code… I do not know your file structure or any details - so I’m not sure if it works as-is… but at least is a start.


too - you could potentially ‘skip’ the whole processFolder step - if you are sure to be working with a single folder that contains everything you need… using it just insures reproducibility if such things change down the road or are different for others using the code.

Thanks for your answer,

After many tries, this is still not working…
I don’t know why.

It seems to fail opening if the path to the file contains a variable.
I even tried this :

file = getInfo("window.contents");

And i get the same error :

File is not in a supported format, a reader plugin is not available, or it was not found.

Is there any way i can get the file name otherwise than “getInfo(“window.contents”);” ?
Or any other solution to open the file ?

When i “print(file);” the txt appearing in log is exactly the path to the file : “C:/myfile.tif”

I don’t understand :upside_down_face:

Any help is appreciated,


So i finally found how to avoid the problem.

	run("Barcode Codec");
	filename = getInfo("window.contents");

	filename2 = parseInt(filename);  //This is what I added

	dir = "C:/";
	tif = ".tif";

After converting the filename to an Integer, it works perfectly, it also works with “toHex” and “toBinary”.
but it doesn’t work with “toString” somehow.

So all my files have to be named as an integer, which in my case, is not a major problem.

Problem solved :slight_smile: