javascriptfilerecursionimagej

Recursive function JavaScript concat array result - ImageJ


I tried to make a JavaScript code for ImageJ, which has to goal to allow me to select a directory, and return me all jpg and jpeg files in the directory and sub-directories.

I have do this code, it works well for direct children, it calls getJpeg on subdirectories (I have verify it with some IJ.log), but the jpeg Array is not concatenated with the results of recursive calls

Have you an idea?

importClass(Packages.ij.IJ);
importClass(Packages.ij.plugin.frame.RoiManager);
importClass(Packages.ij.gui.GenericDialog);
importClass(Packages.ij.io.OpenDialog);
importClass(Packages.java.io.File);

var dirstr = IJ.getDirectory("Choose the stacks folder");
var jpeg = getJpeg(dirstr);

for (var i = 0; i < jpeg.length; i++) {
        IJ.log(jpeg[i]);
}

function getJpeg(dirst) {
        var dir = new java.io.File(dirst);
        var names = dir.listFiles();
        var jpeg = new Array();
        for (var i = 0 ; i < names.length; i++) {
            if(names[i].isDirectory()){
                jpeg.concat(getJpeg(names[i].getPath()));
                continue;
            }
            var namest = names[i].getName();
            if (namest.match(/.*\.jpg|jpeg/)) {     
                    var path = names[i].getPath();
                    jpeg.push(path);
            }
        }
        return jpeg;
}

Thank you


Solution

  • The problem come from a missread from concat documentation ( w3schools.com/jsreF/jsref_concat_array.asp ). Have to put jpeg = jpeg.concat(getJpeg(names[i].getPath()));