Problem deleting images within a macro


I’ve been working on a macro that analyses foci within cells. I have set it to run in batch mode on multiple images, where it will open the first image in my folder, edit it and then finally give me a summary text file.

After I have analysed each image I would like the macro to delete the temporary files it has created whilst analysing it. However, when I add in this code which I have seen many people using to delete images, the images are not deleted and no log file appears:


templist = getFileList(temp);					
for (i=0; i<templist.length; i++) {

If however, I create another separate macro using this exact same piece of code and run it once my main macro has finished creating the images, then the code works and the images are deleted.

Here’s a simplified version of my macro, to demonstrate how the piece of code fits into it (the deletetemp() function):

macro "Foci counter" {

dir = "/Users/***/Desktop/Analysis/Colocalisation/ImageJ/" 
list = getFileList(dir); 						// get all the file names in this directory
temp=dir+"Temp/";								// make a new file path name for a Temp folder
File.makeDirectory(temp);						// make the new Temp folder

setBatchMode(true);								// turn on batch mode to speed things up

for (f=0; f<list.length; f++) {					// iterate over every image in the directory
    openfile();									// invoke the openfile function
    maskbackground();							// invoke the maskbackground function							
    prev=0;										// prev is a count of the previous set of meaurements for each image
    runner=0;									// runner is a running total of measurements in each image
    freememory();								// invoke the free memory function	
    removedeadcells();							// invoke the removedeadcells function
    closeresultswindow();						// invokes the close results window function
    selectfoci();								// invokes count foci function
    deletetemp();								// ***THE DELETE FUNCTION*** deletes temporary images


Does anyone have an idea why this method of deleting files works in a separate macro but not within my current macro??



Hi @Alex_Becalick,

I could first reproduce your problem. Then I figured out that it might be linked to incorrect file separator. The following macro opens the blobs image binarizes it and saves it in 10 versions. Thereafter, it manages to delete those in one and the same macro.
Potentially, you can use that one to include your macro part.

dir = getDirectory("home") + "TEMP";

run("Blobs (25K)");
setOption("BlackBackground", true);
run("Make Binary");
for(i=0; i<10; i++) {
	saveAs("Tiff", dir + File.separator + "blobs-" + i);


fileList = getFileList(dir);
for(i=0; i<fileList.length; i++) {
	print(dir + File.separator + fileList[i]);
	File.delete(dir + File.separator + fileList[i]);


Thanks for the reply, I tried your suggestion but sadly it didn’t change my results. It did make me think about using the print function to diagnose what was going on though. I found that the problem was tracing back to this line:

templist = getFileList(temp);

It seems that templist was being defined incorrectly as an array with nothing in it, rather than giving the file list that you would expect. I found that a few } were missing from the functions defined above my ‘deletetemp’ function, so perhaps this was causing it to be incorrectly defined early, before the temp folder even contained any images.

Whatever it may have been, the deletetemp function now works as I hoped it would and deletes all my images.