I am thinking someone would probably have already wrote this code/macro, but can't find it...
I want to apply a macro to all files within a certain folder and all its subfolders. I can see how this works in theory with getFileList(Directory), but just can't seem to get the code quite rite. Any help would be much appreciated. Rhod |
Hi, I have following codes might help you. You may want to modify a
little bit to suit your need. run("Set Measurements...", "area mean standard min integrated median area_fraction stack limit display redirect=None decimal=3"); run("Clear Results"); dir1 = getDirectory("Choose Directory "); list = getFileList(dir1); setBatchMode(false); for (i=0;i<list.length;i++) { if (endsWith(list[i],"tif") || endsWith(list[i],"TIF")){ open(dir1+ list[i]); run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); run("Measure"); close(); } else if (endsWith(list[i],"/")) { idx2=lengthOf(list[i])-1; dir2=dir1+substring(list[i],0,idx2); list2 = getFileList(dir2); for (j=0;j<list2.length;j++) { if (endsWith(list2[j],"tif") || endsWith(list2[j],"TIF")){ open(dir2+"\\"+ list2[j]); run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); run("Measure"); close(); } } } } -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Wilson R S (AT) Sent: Wednesday, April 06, 2011 9:02 AM To: [hidden email] Subject: Apply to all files... I am thinking someone would probably have already wrote this code/macro, but can't find it... I want to apply a macro to all files within a certain folder and all its subfolders. I can see how this works in theory with getFileList(Directory), but just can't seem to get the code quite rite. Any help would be much appreciated. Rhod |
In reply to this post by Wilson R S (AT)
2011/4/6 Wilson R S (AT) <[hidden email]>:
> I am thinking someone would probably have already wrote this code/macro, but can't find it... > > I want to apply a macro to all files within a certain folder and all its subfolders. I can see how this works in theory with getFileList(Directory), but just can't seem to get the code quite rite. > > Any help would be much appreciated. > > Rhod Considering you want subdirectories as well, I'd do it with a python script: basedir = "/path/to/root/dir/" for root, dirs, files in os.walk(basedir): for file in files: # do something to each file See examples in Jython here: http://pacific.mpi-cbg.de/Jython_Scripting -- http://albert.rierol.net http://www.ini.uzh.ch/~acardona |
In reply to this post by Wilson R S (AT)
Hi Rhod,
I wrote a function some time ago to do it. It returns an array with all files inside a directory with a specific extension and is recursive (also looks inside the subdirectory). It also uses other functions I wrote to manipulate arrays. I pasted it all on http://pastebin.com/wtQMSBwm I'd recommend you place all of them in macros/Library.txt so all your macros can access them. You can call the function as file_list = get_cleansed_file_list (dir, extension); Hope it helps Carnë On 6 April 2011 14:02, Wilson R S (AT) <[hidden email]> wrote: > I am thinking someone would probably have already wrote this code/macro, but can't find it... > > I want to apply a macro to all files within a certain folder and all its subfolders. I can see how this works in theory with getFileList(Directory), but just can't seem to get the code quite rite. > > Any help would be much appreciated. > > Rhod |
In reply to this post by Wilson R S (AT)
Hello,
You are right, you want a recursive loop that uses getFileList, and this macro is already available from the ImageJ website : http://rsbweb.nih.gov/ij/macros/BatchProcessFolders.txt In this macro, the "processFiles" function is the recursive loop that goes through all folders and subfolders. Every time an image file is encountered, the function "processFile" is run. Use of the countFiles function is optional. Normally you simply need to add a few lines in the "processFiles" function for the commands you want to run on each image, and this macro should work for you. Just let me know if you encounter any problem. Kind Regards, Elizabeth Crowell Wilson R S (AT) a écrit : > I am thinking someone would probably have already wrote this code/macro, but can't find it... > > I want to apply a macro to all files within a certain folder and all its subfolders. I can see how this works in theory with getFileList(Directory), but just can't seem to get the code quite rite. > > Any help would be much appreciated. > > Rhod > -- Elizabeth CROWELL ---------------------------------------------------------------------- Membrane Traffic and Cell Division Research Group Institut Pasteur 28 rue du Dr Roux 75015 PARIS, France Tel : 01.44.38.94.07 Fax : 01.45.68.89.54 ---------------------------------------------------------------------- |
Free forum by Nabble | Edit this page |