ImageJ running macro on subfolders

I have recorded a macro in imageJ and want to run it over all specified folders and sub-folders. The key issue is I need to write the data collected out into a new folder, with the output files named after the folder/subfolder they come from (to keep track of the data).

I found the following code for running a macro over folders/subfolders (http://imagej.1557.x6.nabble.com/batch-processing-across-folders-td3693993.html) and have inserted my macro into this. So have the following:

mainDir = getDirectory("Choose a main directory "); 
mainList = getFileList(mainDir); 

for (i=0; i<mainList.length; i++) {  // for loop to parse through names in 
main folder 
     if(endsWith(mainList[i], "/")){   // if the name is a subfolder... 

          subDir = mainDir + mainList[i]; 
          subList = getFileList(subDir); 

           for (j=0; j<subList.length; j++) {  // for loop to parse through 
names in subfolder 

           run("Image Sequence...", "open=[/Users/apple/Documents/1 Fourth year/Project/Data/Experiment 1/Day 0/1Ai] sort");
           run("8-bit");
           run("Z Project...", "projection=[Average Intensity]");
           imageCalculator("Subtract create stack", "1Ai","AVG_1Ai");
           selectWindow("Result of 1Ai");
           run("Color Threshold...");
           run("8-bit");
           setAutoThreshold("Default dark");
           //run("Threshold...");
           setThreshold(21, 255);
           run("Analyze Particles...", "size=70-Infinity circularity=0.00-0.78 show=Outlines display clear stack");
saveAs("Results", "/Users/apple/Documents/Results/Ai.csv");

          } 
     } 
}

I want to be able to run this so that it runs over every folder and subfolder, and saves all the results into:
“/Users/apple/Documents/Results/”

with the name of the folder and subfolder pasted together to form the name of the .csv file which is written out. Is there a way to do this?

Many thanks

Izzy

Maybe something like this would work:


SourceDir = getDirectory("Choose Source Directory ");

TargetDir = getDirectory("Choose Destination Directory ");

list = getFileList(SourceDir);

// Takes a directory containing sub-directories, loads them in sorted order and does an operation on them
for(i = 0; i < list.length; i++) {



     showProgress(i+1, list.length);
     subdir = SourceDir + list[i];
     Myoperation(subdir);
     close();
}

function Myoperation(subdir) {
           run("Image Sequence...", "open=subdir sort");
           title = getTitle();
           run("8-bit");
           run("Z Project...", "projection=[Average Intensity]");
           imageCalculator("Subtract create stack", "1Ai","AVG_1Ai");
           selectWindow("Result of 1Ai");
           run("Color Threshold...");
           run("8-bit");
           setAutoThreshold("Default dark");
           //run("Threshold...");
           setThreshold(21, 255);
           run("Analyze Particles...", "size=70-Infinity circularity=0.00-0.78 show=Outlines display clear stack");
saveAs('.csv', TargetDir + title);

}

If I am understanding your situation correctly, I believe this script is an example of the directory parsing you will want to do.