Login  Register

Re: Recursive processing of a directory not working correctly

Posted by Matthew Jacobs on May 22, 2014; 4:14am
URL: http://imagej.273.s1.nabble.com/Recursive-processing-of-a-directory-not-working-correctly-tp5007797p5007842.html

Thanks to all of you I was able to get my code working well enough to do what I need to do! The code below is working well enough to batch convert a directory full of .vsi files to .tiff using the functions I need! You guys are the best.

Dialog.create("File type");
Dialog.addString("File suffix: ", ".vsi", 5);
Dialog.show();
suffix = Dialog.getString();

inDir = getDirectory("Choose Directory Containing " + suffix + " Files ");
outDir = getDirectory("Choose Directory for TIFF Output ");
setBatchMode(true);
processFiles(inDir, outDir, "");
print("Done!");

function processFiles(inBase, outBase, sub) {
  flattenFolders = true; // this flag controls output directory structure
  print("Processing folder: " + sub);
  list = getFileList(inBase + sub);
  if (!flattenFolders) File.makeDirectory(outBase + sub);
  for (i=0; i<list.length; i++) {
    path = sub + list[i];
    //upath = toUpperCase(path); only need if the file extension is case sensitive
    upath = path; //avoids the previous line
    if (File.isDirectory(path)) {
      processFiles(inBase, outBase, path);
     
    }
    else if (endsWith(upath, suffix)) {

    print("Importing " + suffix + " = " + list[i]);

                run("Bio-Formats Windowless Importer", "open=["+inBase + path+"] color_mode=Default view=Hyperstack stack_order=XYCZT");
               
                print("Stack to Images...");
                run("Stack to Images");
       
                print("Blue...");
                selectImage(1);
                title1 = getTitle();
                run("Blue");

                print("Green...");
                selectImage(2);
                title2 = getTitle();
                run("Green");

                print("Red...");
                selectImage(3);
                title3 = getTitle();
                run("Red");
               
                print("Merging Channels...");
                run("Merge Channels...", "red=&title3 green=&title2 blue=&title1 gray=*None* cyan=*None* magenta=*None* yellow=*None* create keep");

                print("Converting to RGB");
                run("RGB Color");
               
                saveAs("Tiff", outBase + path);
                run("Close All");
    }
  }
}
Matthew Jacobs,
Research Assistant
Callaway Neuroscience Laboratory
The Salk Institute for Biological Studies