Javascript : how to get a (filtered) list of files in a directory

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Javascript : how to get a (filtered) list of files in a directory

lechristophe
Hi,

I'd like to for my javascript (JS) to scan into a user-defined folder
for all tif images. I could'nt find an exemple for that on IJ's or
Fiji's website, and I have two very basic questions:

- what is the simplest way to get the list of files in a given folder
in IJ javascript? In my implementation, it uses the java File class
and the listFiles() method. However in this method one have to be
careful to switch between "strings" and "file" types when using them
in JS. Is there a simpler way ?

- How can I filter the result to only have the files that end with
".tiff" ? I did it manually in my implementation (testing an array
obtained by listFiles into another array), but is it possible to use a
Java Filenamefilter or Filefilter? Or would it be an overkill in JS ?
Here also I have to switch between file and string types to test for
the names.

Here is my code so far:

importClass(Packages.java.io.File)

var dirstr = IJ.getDirectory("Choose the stacks folder");
var tiffs = getTiffs(dirstr);

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

function getTiffs(dirst) {
        var dir = new java.io.File(dirst);
        var names = dir.listFiles();
        var tiffs = new Array();
        for (var i = 0 ; i < names.length; i++) {
                var namest = names[i].getName();
                var ext = namest.substring(namest.length()-4, namest.length());
                if (ext == ".tif" || ext == "tiff") {
                        var path = names[i].getPath();
                        tiffs.push(path);
                }
        }
        return tiffs;
}


Thanks to let me know how I can make it better!

Christophe
Reply | Threaded
Open this post in threaded view
|

Re: Javascript : how to get a (filtered) list of files in a directory

ctrueden
Hi Christophe,

Thanks to let me know how I can make it better!
>

Your approach looks good to me. The only thing I would change is the way
you are checking the file extension.

   var namest = names[i].getName();
>    var ext = namest.substring(namest.length()-4, namest.length());
>       if (ext == ".tif" || ext == "tiff") {
>

Some problems with the above:
* A file named "stiff" would be (erroneously?) detected as a TIFF file.
* A file named "DATA.TIF" would not be detected, due to differences in
capitalization.
* A file called "x" would cause an error, because length()-4 equals -3.

Here is another way, that avoids these issues:

   var namest = names[i].getName().toLowerCase();
      if (namest.endsWith(".tif") || namest.endsWith(".tiff")) {

HTH,
Curtis

P.S. One of the things you can do with Bio-Formats is ask it: for a
particular file, what format is it? This is much more sophisticated than
checking the file extension, because BF will open the file if necessary to
examine its structure. In the case of TIFF however, it would be overkill; I
only suggest going that route if you are trying to identify a more
difficult-to-discern format, a variety of file formats, or answer the
question: for a particular file, is it in *any* supported format at all?


On Sun, Mar 18, 2012 at 9:44 AM, Christophe Leterrier <
[hidden email]> wrote:

> Hi,
>
> I'd like to for my javascript (JS) to scan into a user-defined folder
> for all tif images. I could'nt find an exemple for that on IJ's or
> Fiji's website, and I have two very basic questions:
>
> - what is the simplest way to get the list of files in a given folder
> in IJ javascript? In my implementation, it uses the java File class
> and the listFiles() method. However in this method one have to be
> careful to switch between "strings" and "file" types when using them
> in JS. Is there a simpler way ?
>
> - How can I filter the result to only have the files that end with
> ".tiff" ? I did it manually in my implementation (testing an array
> obtained by listFiles into another array), but is it possible to use a
> Java Filenamefilter or Filefilter? Or would it be an overkill in JS ?
> Here also I have to switch between file and string types to test for
> the names.
>
> Here is my code so far:
>
> importClass(Packages.java.io.File)
>
> var dirstr = IJ.getDirectory("Choose the stacks folder");
> var tiffs = getTiffs(dirstr);
>
> for (var i = 0; i < tiffs.length; i++) {
>        IJ.log(tiffs[i]);
> }
>
> function getTiffs(dirst) {
>        var dir = new java.io.File(dirst);
>        var names = dir.listFiles();
>        var tiffs = new Array();
>        for (var i = 0 ; i < names.length; i++) {
>                var namest = names[i].getName();
>                var ext = namest.substring(namest.length()-4,
> namest.length());
>                if (ext == ".tif" || ext == "tiff") {
>                        var path = names[i].getPath();
>                        tiffs.push(path);
>                }
>        }
>        return tiffs;
> }
>
>
> Thanks to let me know how I can make it better!
>
> Christophe
>