1

I have a nested file structure where a parent folder contains multiple folders of different types of data. I am using an ImageJ macro script to batch process all of the image files within one of those folders. I currently need to process each folder separately, but I would like to batch process over the folders. I have looked up some batch processing of multiple folders, but it appears that the code is processing all folders and files within all of the folders. I only need to process one folder within each directory (all named the same). The images come from the instrument without any metadata, so the files are saved as such to separate the experiments, where all data for the experiment is contained within the parent folder. Also, I have two different scripts that I need to run, one after the other. It would be great if I could merge those, but I am don't know how to do that either.

An example of the structure is:

  • Experiment1/variable1/processed
  • Experiment1/variable2/processed

I am currently running my macro on each of the "processed" folders individually. I would like to batch each "processed" folder within each of the "variable" folders.

Any help would be greatly appreciated, I am really new to coding and am just really trying to learn and automate as much as possible.

Thank you!

2 Answers2

2

Did you try the batch processing scripts you came across? Reading the batch processing example which is provided with ImageJ leads me to believe it would work for your example. If you haven't tested it, you should do so (you can put in a command like "print(list[i])" in the place of your actual macro while you test that you've got the file finding section working.

To merge two different scripts, the simplest option would be to make them individual functions. i.e.:

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
    list = getFileList(input);
    list = Array.sort(list);
    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))
            processFile(input, output, list[i]);
            processOtherWay(input, output, list[i]);
    }
}

function processFile(input, output, file) {
    // Do the processing here by adding your own code.
    // Leave the print statements until things work, then remove them.
    print("Processing: " + input + File.separator + file);
    print("Saving to: " + output);
}

function processOtherWay(input, output, file) {
    // Do the processing here by adding your own code.
    // Leave the print statements until things work, then remove them.
    print("Processing: " + input + File.separator + file);
    print("Saving to: " + output);
}

If the goal isn't to run them on the exact same image, then again make them standalone functions, and have the folder sorting section of the script be in two parts, one for function 1, one for function 2.

J. Kovacs
  • 157
  • 1
  • 1
  • 10
1

You can always just take the code you have and nest it in another for loop or two.

numVariables = ;//number of folders of interest

for(i = 1; i <= numVariables; i++) //here i starts at 1 because you indicated that is the first folder of interest, but this could be any number
{
    openPath = "Experiment1/variable" + i + "/processed";
    files = getFileList(openPath);
    for(count = 0; count < files.length; count++) //here count should start at 0 in order to index through the folder properly (otherwise it won't start at the first file in the folder)
    {
        //all your other code, etc.
    }
}

That should just about do it I think.

ekiely
  • 92
  • 8
  • I have no idea what I'm doing. I tried your suggestion, but I can't get it to work. I don't know what goes in the "all your other code" so that it will iterate through the list and open an image, do the stuff, then save it. It just keeps opening the same image over and over. Do you have any idea where I am going wrong? – ashcrashbegash Nov 19 '19 at 21:16
  • I'd love to help. Would you mind providing the code that you are currently using so I have a better idea of what you're working with right now? – ekiely Nov 20 '19 at 16:48