Directory and subdirectory listening

I am searching for a way to make an imagej macro run whenever a new excel file is created in any of an expanding list of subdirectories. I found this program online that works perfectly with one single directory:

-- snipsnap --
directory = getDirectory("Select the directory");

// get the initial list (without opening the files)
list = getFileList(directory);
Array.sort(list);

for (;;) {
        previousList = list;
        list = getFileList(directory);
        if (previousList.length != list.length) {
                Array.sort(list);

                // walk both lists
                j = 0;
                for (i = 0; i < list.length; i++) {
                        stopJ = previousList.length;
                        while (j < stopJ)
                                if (previousList[j] < list[i])
                                        j++;
                                else
                                        stopJ = j;
                        if (j >= previousList.length)
                                open(directory + list[i]);
                        else if (list[i] != previousList[j])
                                open(directory + list[i]);
                }
        }

        wait(1); // wait a second
}

I took this code and tried to make it iterate through all subdirectories as follows:
directory = “C:\example\parent\folder”;

 // get the initial list (without opening the files)
 list = getFileList(directory);
 Array.sort(list);

 for (;;) {
	for(k = 0; k < list.length; k ++)
	{
		list[k] = substring(list[k], 0, 6);
		print(directory + File.separator + list[k]);
		if(File.isDirectory(directory + File.separator + list[k]))
		{
			sublist = getFileList(directory + File.separator + list[k]);
			subDir = directory + File.separator + list[k];
			compareSubDir(sublist, subDir);
		}
		if(k == list.length)
		{
			k = 0;
		}
 	}
 }

 function compareSubDir(list, directory) { 
 	    previousList = list;
        list = getFileList(directory);
        Array.print(list);
        if (previousList.length != list.length) {
                Array.sort(list);

                // walk both lists
                j = 0;
                for (i = 0; i < list.length; i++) {
                        stopJ = previousList.length;
                        while (j < stopJ)
                                if (previousList[j] < list[i])
                                        j++;
                                else
                                        stopJ = j;
                        if (j >= previousList.length)
                                print("hello");
                        else if (list[i] != previousList[j])
                                print("hello");
                }
        }

       // wait a second
 }
// function description

This does not seem to pick up on any new files at all. Of course, this method is extremely slow and inefficient being that it iterates through all files in each subdirectory when only about 1/3rd of them are excel files. How can I make this more efficient? Is there a way I can have multiple watching macros running at once?

As far as not processing the wrong files, you may want to have a look at the built in ImageJ macros that come in the Templates menu.

1 Like