Automate tasks using macros in images placed in folders and subfolders

imagej

#21

Hi @7rebor
I select the output directory at the begenning, and when the script runs, it creates / replicates the folders of the input folder, but only the first level of the hierarchy. I mean, in the example above-mentioned it creates Subfolder 1, 2 and 3, but fails creating the Sub-subfolder 1 placed inside Subfolder 3, and I think this is de error.


#22

Hi @antecessor,

My suspicion is that any folder in the structure below a subfolder will not be created, the feeling here is that the script runs through to the lowest branch of the folder tree first (because as soon as it finds a directory instead of a file, it works on that directory). See:

if(File.isDirectory(input + File.separator + list[i])) {
    processFolder("" + input + File.separator + list[i]);
}

So it is trying to make directories inside ones that don’t exist (I don’t know if this is the problem), i.e. the directory “output\first\second” is being made before “output\first”.

The way I solved this potential problem was to create the output directory as soon as the input directory is found, see code below:

    #@File(label = "Input directory", style = "directory") input
    #@File(label = "Output directory", style = "directory") output
    #@String(label = "File suffix", value = ".tif") suffix


    var outputDir // global variable to be passed to function later
    var inputDir 
    outputDir = File.getName(output); // retrieves the folder name string of the master input folder selected by the user above
    inputDir = File.getName(input);	
    
	processFolder(input);
    	
    // function to scan folders/subfolders/files to find files with correct suffix
    function processFolder(input) {
    	list = getFileList(input);
    	for (i = 0; i < list.length; i++) {
    		if(File.isDirectory(input + File.separator + list[i])) {
    			tempInputDir = input + File.separator + list[i];
    			saveDir = replace(tempInputDir, inputDir, outputDir); // replaces the input folder name (string) with the output folder name (string)
    			File.makeDirectory(saveDir);
    			processFolder("" + input + File.separator + list[i]);
    		}
    		if(endsWith(list[i], suffix)) {
    			processFile(input, output, list[i]);
                i = list.length;
    		}
    	}
    }

    function processFile(input, output, file) {
    	run("Image Sequence...", "open=[input] number=304 starting=1 increment=1");
    	run("8-bit");
    	run("Sharpen", "stack");
    	run("Enhance Contrast...", "saturated=0.3");
    	makeRectangle(540, 354, 878, 746);
    	run("Align slices in stack...", "method=5 windowsizex=620 windowsizey=454 x0=237 y0=138 swindow=0 subpixel=false itpmethod=0 ref.slice=1 show=true");
    	run("Z Project...", "projection=[Average Intensity]");
    	
        // save the images following the same input directory folders
    	saveDir = replace(input, inputDir, outputDir); // replaces the input folder name (string) with the output folder name (string)
    	File.makeDirectory(saveDir); // makes the above directory
    	saveAs("TIFF", saveDir + file);
    	close();
    }

See how this goes.

Best,

Rob


#23

I got to solve the problem by adding this line to the script
if(endsWith(list[i], suffix)) {
processFile(input, output, list[i]);
i = list.length;
I found later a problem but it was because some folders did not have files inside, and that made the script to fail. Thanks


#24

Sorry, just to confirm, did you take i=list.length out of the if statement?

Rob