Macro for skip/don't open if file not present in image sequence



Hello everybody,

I wrote a macro to open a sequence of file containg a certain name in the tile and than further processing it with several tools. It works very well but I am encountering the problem that if a file is not present a error pops up and the analysis stops. Is there a way to tell it “skip if file not present”?
That’s my macro:

dir1 = getDirectory("Choose Source Directory ");
outputDir= getDirectory("")

run("Image Sequence...", "open=[dir1] file=F1- sort");
{ run("Extended Depth of Field (Easy mode)...", "quality='0' topology='0' show-topology='off' show-view='off'"); 

 initTime = getTime(); 
 oldTime = initTime; 
 while (!isOpen("Output")) { 
     elapsedTime = getTime() - initTime; 
     newTime = getTime() - oldTime; 
     if (newTime > 10000) { 
         oldTime = getTime(); 
         newTime = 0; 
         print(elapsedTime/1000, " seconds elapsed"); 
edfTitle = getTitle(); 
saveAs("Tiff", outputDir + edfTitle);
run("Close All");

And than I repeat it for all the different name I want e.g. F2, A1, C3 etc etc,
What I would like to add is at the beginning do not open the file if not present, so that I can randomly apply it to all my folders.
I have tried with
"open=[dir1] file=F1- sort if(File.exists)"
but it doesn’t work.

Do you know how to do that?

Thank you very much



So… what you could do is modify your code a bit to use Script Parameters… that will help you get your directory info and you can use it for your naming issue…

So something like this:

#@File(label = "Input directory", style = "directory") input
#@File(label = "Output directory", style = "directory") output
#@String(label = "File name contains", value = "F2") subString

// function to scan folders/subfolders/files to find files that contain subString
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(indexOf(list[i], subString) >= 0)
			processFile(input, output, list[i]);

function processFile(input, output, file) {
// insert the code you want to run on each image here

NOTE: YOU WILL NEED TO MODIFY THE ABOVE CODE… I was unable to run it myself on any test datasets using your specific code… so test it and edit as you need.

Hope this helps a bit.

eta :slight_smile:


Dear etarena,

thank you very much but this is also not really working: if a file is not present in the folder an errore bar pops up.
Is there a way to say “ignore them and keep going on”?

Thank you very much for your time




I did not get an error when I ran the code I posted for my own dumbie set of images in folders/subfolders. I am using the exact code that I pasted for you above… so if you modified something - then I am not sure why you are getting an error.

You could include an else statement after the if(indexOf(list[i], subString) >= 0) to deal with scenarios when a match is not found…

Perhaps you can share the error, as well as the exact code you are using?



You are so right: I did something wrong when pasting the code!! So sorry!! :cry:

thank you so much, this is really helping and I apologise for my bad “programming” but I’ve just started with it and thus never used string before. :blush:

I seize the opportunity to ask you something else: what I am doing with the code that I posted above is: open all the z-stack for the image F2, do a projection and focus it (extended depth). I actually have more files in the same folder from F1 to F12 (15 files each). So far, I copied/pasted the code and manually changed “sort for F1 (at the begininng) and rename (at the end”) for each different number.
I don’t think so but is there a way to write the code only once and tell “it” repeat it for F1, F2, F3, F4 etc. etc.?
Did I explain myself? :confused:

Thank you so so much:)




No worries. Take your time with coding… and play with it - see what changes, etc. That’s the fun part! :slight_smile:

You can look into using loops… most likely a for loop. I don’t quite know how you’ve organized your images in folders, subfolders, etc. so I cannot help you in too much detail at this point. But read up on for loops… try something and post example code.

The more information/details you can provide us (including folder structure and your code) - the better we can help you. But as always - try yourself first! That’s the best way to learn.



Is it possible to change the value =“F2” in the parameters to be a variable substring? I have two folder of Images. One is masks of a particular feature found in the Images in the other folder, but not all images have masks so my two folders do not have the same number of images. I want to write a code that says
In Folder A, open file, find file with matching substring (Entire image names will not be the same) and if so preform X operation. In the example above the substring seems to be fixed not a continually changing unique identifier to a pair of images that for my purposed need to be kept in sync.


Yes - that seems possible. It’s been awhile since I’ve visited this topic… so you’ll have to trouble-shoot a bit on your own, playing with the code. I still do not know how your files are saved exactly - your file structure - so it’s a bit difficult for me to give you an exact answer.

You’ll have to play with the code yourself - use print statements of the filenames within as you test things. Remember - you can just check if the filename contains any substring - use the Built-In Macro Functions list to search for appropriate commands.