Recursively lists

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

Recursively lists

Johannes Breu
Hello,

version A works.

*Version A*
// Recursively lists the files in a user-specified directory
// With ImageJ 1.37i or later, open files on the list by
// double clicking on them. A preview of 1.37i is available at
//   http://rsb.info.nih.gov/ij/ij.jar

  requires("1.32f");
  dir = getDirectory("Choose a Directory ");
  count = 1;
  listFiles(dir);

  function listFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              listFiles(""+dir+list[i]);
          else
              print((count++) + ": " + dir + list[i]);
      }
  }

If I address dir to another variable it doesn´t work. Why?

*Version B*
// Recursively lists the files in a user-specified directory
// With ImageJ 1.37i or later, open files on the list by
// double clicking on them. A preview of 1.37i is available at
//   http://rsb.info.nih.gov/ij/ij.jar

  requires("1.32f");
  dir = getDirectory("Choose a Directory ");

  count = 1;
  listFiles(dir);

  function listFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              listFiles(""+dir+list[i]);
          else
              *m=dir;*
              print((count++) + ": " *+ m* + list[i]);
      }
  }

Actually I would like to have the possibility to treat *dir or m* as an
variable becaus I wish to extract a substring out of *dir*.


Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Recursively lists

jmutterer
Johannes,
you should include all the else statement between curly braces :

 if (endsWith(list[i], "/"))
           listFiles(""+dir+list[i]);
 else {
             m=dir;
             print((count++) + ": " + m + list[i]);
  }


the braces can be omitted if the statement carries only one instruction.

Jerome

On Thu, Sep 18, 2008 at 6:52 PM, Johannes Breu <[hidden email]> wrote:

> Hello,
>
> version A works.
>
> *Version A*
> // Recursively lists the files in a user-specified directory
> // With ImageJ 1.37i or later, open files on the list by
> // double clicking on them. A preview of 1.37i is available at
> //   http://rsb.info.nih.gov/ij/ij.jar
>
>  requires("1.32f");
>  dir = getDirectory("Choose a Directory ");
>  count = 1;
>  listFiles(dir);
>
>  function listFiles(dir) {
>      list = getFileList(dir);
>      for (i=0; i<list.length; i++) {
>          if (endsWith(list[i], "/"))
>              listFiles(""+dir+list[i]);
>          else
>              print((count++) + ": " + dir + list[i]);
>      }
>  }
>
> If I address dir to another variable it doesn´t work. Why?
>
> *Version B*
> // Recursively lists the files in a user-specified directory
> // With ImageJ 1.37i or later, open files on the list by
> // double clicking on them. A preview of 1.37i is available at
> //   http://rsb.info.nih.gov/ij/ij.jar
>
>  requires("1.32f");
>  dir = getDirectory("Choose a Directory ");
>
>  count = 1;
>  listFiles(dir);
>
>  function listFiles(dir) {
>      list = getFileList(dir);
>      for (i=0; i<list.length; i++) {
>          if (endsWith(list[i], "/"))
>              listFiles(""+dir+list[i]);
>          else
>              *m=dir;*
>              print((count++) + ": " *+ m* + list[i]);
>      }
>  }
>
> Actually I would like to have the possibility to treat *dir or m* as an
> variable becaus I wish to extract a substring out of *dir*.
>
>
> Thanks
>
Reply | Threaded
Open this post in threaded view
|

Re: Recursively lists

ed3203
Hi, sorry to bump this; but i thought it proper to include information on the recursive file listing script to be kept together.

What is the purpose of the "" in the script? I can't figure out why this is required
listFiles(""+ dir+list[i]);

cheers,

ed

Reply | Threaded
Open this post in threaded view
|

solution to "" in 'Recursively list'

ed3203
hi,
just posting the answers I've received so that the question doesn't get asked over and over again by others with the same query!

"If you don't have the "" at the beginning, sometimes, it will try to convert "dir" and "list[i]" to numbers and add them together.  I'm not sure under what conditions that occurs...  But, by starting with "", it must somehow force it to be text instead of a number. "

"I suspect the "" is there to work around a bug in the macro interpreter. It forces the interpreter to concatenate the dir and list[i] string variables. Without the "" the interpreter attempts to add the two variables, which creates a NaN (not a number) numeric result."

Thanks to Wayne and Jeff!!

ed