Hi,
I want to sort files into specific folders. I have three images for one histological section, that i would like to stitch them later. For instance, E8_001_001, E8_001_002, E8_001_003 (all these three image compose one section), and then E8_002_001...and so on. I was able to move the first image into the folder name as the first image file individually, such as E8_001_001, E8_002_001. Then I want to select the every other image that has _002 as last number and move these into the folder that is associated with first image. For instance, I want to move E8_001_002 image into the E8_001_001 folder that has E8_001_001 image. So i have wirten the code below,and this works if i rename the every second image to 1. But i would like to keep the original name and just want to move those file into a new folder. But if i add list[i] in the newpath, it moves all the second image into the first folder. How can fix this? dir = getDirectory("Choose main Directory");///this contains the subdirectory, and tiffs dir2 = getDirectory("choose the subdirectory folder"); ///this contains the folder specific to each section list = getFileList(dir); list2 = getFileList(dir2); print("number of files in the Directory ="+ list.length); n=1; for (i=1; i<list.length; i+=2 ) { if (endsWith(list[i], ".tif")){ print(list[i]); for (k=0; k<list2.length; k++){ if (endsWith(list2[k], "/")){ oldpath= dir + File.separator+ list[i]; newpath = dir2 + File.separator+ list2[k] + File.separator+ n+".tif"; ///this renames files to 1 print(newpath); print(oldpath); File.rename(oldpath, newpath); } }}} Thank you in advance, Rui -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Dear Rui,
Maybe the code below does what you want (not tested), if I understand correctly what you have in mind. I am not totally sure about this as you have in your code: for (i=1; i<list.length; i+=2) what would take image nr 1, 3, 5 etc and not 1, 4, 7 what I would expect seeing your description what would be: for (i=1; i<list.length; i+=3). So maybe I misunderstand what you try to do. ------------------------------------------------------------------------------------- dir = getDirectory("Choose main Directory");///this contains the subdirectory, and tiffs dir2 = getDirectory("choose the subdirectory folder"); ///this contains the folder specific to each section list = getFileList(dir); list2 = getFileList(dir2); print("number of files in the Directory ="+ list.length); for (i=1; i<list.length; i++ ) { if (endsWith(list[i], ".tif")){ print(list[i]); for (k=0; k<list2.length; k++){ if (endsWith(list2[k], "/")){ for (n=1; n<4; n++){ oldpath= dir + File.separator+ list[i]; newpath = dir2 + File.separator+ list2[k] + File.separator+ n+".tif"; ///this renames files to 1 print(newpath); print(oldpath); File.rename(oldpath, newpath); i +=1; } } } } } ------------------------------------------------------------------------------------- Best wishes Kees Dr Ir K.R. Straatman Senior Experimental Officer Centre for Core Biotechnology Services Unversity of Leicester http://www.le.ac.uk/biochem/microscopy/home.html ImageJ workshops 16 and 17 December: http://www.le.ac.uk/biochem/microscopy/ImageJ2013.html -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of thr rui Sent: 01 November 2013 06:23 To: [hidden email] Subject: Select every other image and move into specific folder Hi, I want to sort files into specific folders. I have three images for one histological section, that i would like to stitch them later. For instance, E8_001_001, E8_001_002, E8_001_003 (all these three image compose one section), and then E8_002_001...and so on. I was able to move the first image into the folder name as the first image file individually, such as E8_001_001, E8_002_001. Then I want to select the every other image that has _002 as last number and move these into the folder that is associated with first image. For instance, I want to move E8_001_002 image into the E8_001_001 folder that has E8_001_001 image. So i have wirten the code below,and this works if i rename the every second image to 1. But i would like to keep the original name and just want to move those file into a new folder. But if i add list[i] in the newpath, it moves all the second image into the first folder. How can fix this? dir = getDirectory("Choose main Directory");///this contains the subdirectory, and tiffs dir2 = getDirectory("choose the subdirectory folder"); ///this contains the folder specific to each section list = getFileList(dir); list2 = getFileList(dir2); print("number of files in the Directory ="+ list.length); n=1; for (i=1; i<list.length; i+=2 ) { if (endsWith(list[i], ".tif")){ print(list[i]); for (k=0; k<list2.length; k++){ if (endsWith(list2[k], "/")){ oldpath= dir + File.separator+ list[i]; newpath = dir2 + File.separator+ list2[k] + File.separator+ n+".tif"; ///this renames files to 1 print(newpath); print(oldpath); File.rename(oldpath, newpath); } }}} Thank you in advance, Rui -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
I did not describe clearly.
Essentially I have many images in main folder and images are consist of three images as one set. These one set of three images (E8_001_001, E8_001_002, E8_001_003) need to go one folder. Then next one set of three images (ie. E8_002_001, E8_002_002, E8_002_003) need to go another folder. I want to keep original image file name and the folder name can be same as first image file (E8_001_001, E8_002_001..). If i could do this as one code that would be great. But i do not know how. so i separate code as three steps: first code; select every first image out of three and create the individual folder second code: select every second image out of three and move them into the folder that is associated with the first image third code: select every third image out of three and move them into the folder that is associated with first and second image. (i don't have this yet since the second code has problem of the file name) I have the other code before this as you suggested to use i+=3 instead of 2, as first code as below: //Get the directory dir = getDirectory("Choose a Directory "); SaveDir = getDirectory("Choose a Sort Save Directory "); //Create file list list = getFileList(dir); print("number of files in the Directory ="+ list.length); //Look throug the list - and move the files to for (i=0; i<list.length; i+=3 ) { if (endsWith(list[i], "/")){ //listFiles(""+SubDirNames[0]+list[i]); }else{ //Split the Name by "." and look at the first Entry Data = split(list[i],"."); // Make directory name DirName = Data[0]; //Ceate that directory File.makeDirectory(SaveDir+File.separator+ DirName); // Move the file to that Dir File.rename(dir + list[i],SaveDir + DirName + File.separator + list[i]); } } Then the second code is the one i listed as previous post: dir = getDirectory("Choose main Directory");///this contains the subdirectory, and tiffs dir2 = getDirectory("choose the subdirectory folder"); ///this contains the folder specific to each section list = getFileList(dir); list2 = getFileList(dir2); print("number of files in the Directory ="+ list.length); n=1; for (i=1; i<list.length; i+=2 ) { if (endsWith(list[i], ".tif")){ print(list[i]); for (k=0; k<list2.length; k++){ if (endsWith(list2[k], "/")){ oldpath= dir + File.separator+ list[i]; newpath = dir2 + File.separator+ list2[k] + File.separator+ n+".tif"; ///this renames files to 1 print(newpath); print(oldpath); File.rename(oldpath, newpath); } }}} The problem here is the file name. I cannot keep the original file name and move those into the associated folders. I appreciate any suggestion. Rui |
Rui - I suggest that you change the case of the filename for the directory, because that is what is causing you trouble - you shouldn't duplicate file names, with one a directory and the other an ordinary file. If your ordinary files are E8-something, make the directory e8-something, or vice-versa. alternatively, you could add a leading or trailing string, i.e. D_E8-something.
- Jim On Nov 1, 2013, at 1:21 PM, rtas <[hidden email]> wrote: > I did not describe clearly. > Essentially I have many images in main folder and images are consist of > three images as one set. > These one set of three images (E8_001_001, E8_001_002, E8_001_003) need to > go one folder. > Then next one set of three images (ie. E8_002_001, E8_002_002, E8_002_003) > need to go another folder. > I want to keep original image file name and the folder name can be same as > first image file (E8_001_001, E8_002_001..). > If i could do this as one code that would be great. But i do not know how. > so i separate code as three steps: > first code; select every first image out of three and create the individual > folder > second code: select every second image out of three and move them into the > folder that is associated with the first image > third code: select every third image out of three and move them into the > folder that is associated with first and second image. (i don't have this > yet since the second code has problem of the file name) > > > I have the other code before this as you suggested to use i+=3 instead of 2, > as first code as below: > > //Get the directory > dir = getDirectory("Choose a Directory "); > SaveDir = getDirectory("Choose a Sort Save Directory "); > > //Create file list > list = getFileList(dir); > print("number of files in the Directory ="+ list.length); > > //Look throug the list - and move the files to > for (i=0; i<list.length; i+=3 ) { > if (endsWith(list[i], "/")){ > //listFiles(""+SubDirNames[0]+list[i]); > }else{ > //Split the Name by "." and look at the first Entry > Data = split(list[i],"."); > > // Make directory name > DirName = Data[0]; > > //Ceate that directory > File.makeDirectory(SaveDir+File.separator+ > DirName); > > // Move the file to that > Dir > File.rename(dir + list[i],SaveDir + DirName > + File.separator + list[i]); > > } > } > > > > > Then the second code is the one i listed as previous post: > > dir = getDirectory("Choose main Directory");///this contains the > subdirectory, and tiffs > dir2 = getDirectory("choose the subdirectory folder"); ///this contains the > folder specific to each section > > > list = getFileList(dir); > list2 = getFileList(dir2); > print("number of files in the Directory ="+ list.length); > n=1; > > for (i=1; i<list.length; i+=2 ) { > if (endsWith(list[i], ".tif")){ > print(list[i]); > for (k=0; k<list2.length; k++){ > if (endsWith(list2[k], "/")){ > > > oldpath= dir + File.separator+ list[i]; > newpath = dir2 + File.separator+ list2[k] + > File.separator+ n+".tif"; ///this renames files to 1 > print(newpath); > print(oldpath); > > File.rename(oldpath, newpath); > > } > }}} > > The problem here is the file name. I cannot keep the original file name and > move those into the associated folders. > I appreciate any suggestion. > > Rui > > > > -- > View this message in context: http://imagej.1557.x6.nabble.com/Select-every-other-image-and-move-into-specific-folder-tp5005416p5005423.html > Sent from the ImageJ mailing list archive at Nabble.com. > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by rtas
Hi Rui,
The following macro seems to work if your naming scheme stays the same as your examples.....that is if E8_001 designates a unique data set and needs its own folder labeled E8_001_001, etc. Best, George //Get the directory dir = getDirectory("Choose a Directory "); SaveDir = getDirectory("Choose a Sort Save Directory "); //Create file list list = getFileList(dir); print("number of files in the Directory ="+ list.length); Data2="somestring";//just make a variable to use in checking file names //Look through the list - and move the files to for (i=0; i<list.length; i++ ) { if (endsWith(list[i], "/")){ //listFiles(""+SubDirNames[0]+list[i]); }else{ //Split the Name by "." and look at the first Entry Data = split(list[i],"."); findDataSet=substring(Data[0],0,lengthOf(Data[0])-4);// remove the 001, 002, 003 designations if(findDataSet!=Data2){ // Make directory name DirName = Data[0]; //Ceate that directory File.makeDirectory(SaveDir+File.separator+DirName); // Move the file to that Dir File.rename(dir + list[i],SaveDir + DirName + File.separator + list[i]); Data2=findDataSet; }else{ File.rename(dir + list[i],SaveDir + DirName + File.separator + list[i]); } } } On Fri, Nov 1, 2013 at 1:21 PM, rtas <[hidden email]> wrote: > I did not describe clearly. > Essentially I have many images in main folder and images are consist of > three images as one set. > These one set of three images (E8_001_001, E8_001_002, E8_001_003) need to > go one folder. > Then next one set of three images (ie. E8_002_001, E8_002_002, E8_002_003) > need to go another folder. > I want to keep original image file name and the folder name can be same as > first image file (E8_001_001, E8_002_001..). > If i could do this as one code that would be great. But i do not know how. > so i separate code as three steps: > first code; select every first image out of three and create the individual > folder > second code: select every second image out of three and move them into the > folder that is associated with the first image > third code: select every third image out of three and move them into the > folder that is associated with first and second image. (i don't have this > yet since the second code has problem of the file name) > > > I have the other code before this as you suggested to use i+=3 instead of > 2, > as first code as below: > > //Get the directory > dir = getDirectory("Choose a Directory "); > SaveDir = getDirectory("Choose a Sort Save Directory "); > > //Create file list > list = getFileList(dir); > print("number of files in the Directory ="+ list.length); > > //Look throug the list - and move the files to > for (i=0; i<list.length; i+=3 ) { > if (endsWith(list[i], "/")){ > //listFiles(""+SubDirNames[0]+list[i]); > }else{ > //Split the Name by "." and look at the first Entry > Data = split(list[i],"."); > > // Make directory name > DirName = Data[0]; > > //Ceate that directory > File.makeDirectory(SaveDir+File.separator+ > DirName); > > // Move the file to that > Dir > File.rename(dir + list[i],SaveDir + DirName > + File.separator + list[i]); > > } > } > > > > > Then the second code is the one i listed as previous post: > > dir = getDirectory("Choose main Directory");///this contains the > subdirectory, and tiffs > dir2 = getDirectory("choose the subdirectory folder"); ///this contains the > folder specific to each section > > > list = getFileList(dir); > list2 = getFileList(dir2); > print("number of files in the Directory ="+ list.length); > n=1; > > for (i=1; i<list.length; i+=2 ) { > if (endsWith(list[i], ".tif")){ > print(list[i]); > for (k=0; k<list2.length; k++){ > if (endsWith(list2[k], "/")){ > > > oldpath= dir + File.separator+ list[i]; > newpath = dir2 + File.separator+ list2[k] > + > File.separator+ n+".tif"; ///this renames files to 1 > print(newpath); > print(oldpath); > > File.rename(oldpath, newpath); > > } > }}} > > The problem here is the file name. I cannot keep the original file name and > move those into the associated folders. > I appreciate any suggestion. > > Rui > > > > -- > View this message in context: > http://imagej.1557.x6.nabble.com/Select-every-other-image-and-move-into-specific-folder-tp5005416p5005423.html > Sent from the ImageJ mailing list archive at Nabble.com. > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Yes - better.
- Jim On Nov 1, 2013, at 4:04 PM, George Patterson <[hidden email]> wrote: > Hi Rui, > The following macro seems to work if your naming scheme stays the same as > your examples.....that is if E8_001 designates a unique data set and needs > its own folder labeled E8_001_001, etc. > Best, > George > > > > //Get the directory > dir = getDirectory("Choose a Directory "); > SaveDir = getDirectory("Choose a Sort Save Directory "); > > //Create file list > list = getFileList(dir); > print("number of files in the Directory ="+ list.length); > > Data2="somestring";//just make a variable to use in checking file names > > //Look through the list - and move the files to > for (i=0; i<list.length; i++ ) { > if (endsWith(list[i], "/")){ > //listFiles(""+SubDirNames[0]+list[i]); > }else{ > //Split the Name by "." and look at the first Entry > Data = split(list[i],"."); > findDataSet=substring(Data[0],0,lengthOf(Data[0])-4);// remove the 001, > 002, 003 designations > if(findDataSet!=Data2){ > // Make directory name > DirName = Data[0]; > //Ceate that directory > File.makeDirectory(SaveDir+File.separator+DirName); > // Move the file to that Dir > File.rename(dir + list[i],SaveDir + DirName + File.separator + list[i]); > Data2=findDataSet; > }else{ > File.rename(dir + list[i],SaveDir + DirName + File.separator + list[i]); > } > } > } > > > > > > > On Fri, Nov 1, 2013 at 1:21 PM, rtas <[hidden email]> wrote: > >> I did not describe clearly. >> Essentially I have many images in main folder and images are consist of >> three images as one set. >> These one set of three images (E8_001_001, E8_001_002, E8_001_003) need to >> go one folder. >> Then next one set of three images (ie. E8_002_001, E8_002_002, E8_002_003) >> need to go another folder. >> I want to keep original image file name and the folder name can be same as >> first image file (E8_001_001, E8_002_001..). >> If i could do this as one code that would be great. But i do not know how. >> so i separate code as three steps: >> first code; select every first image out of three and create the individual >> folder >> second code: select every second image out of three and move them into the >> folder that is associated with the first image >> third code: select every third image out of three and move them into the >> folder that is associated with first and second image. (i don't have this >> yet since the second code has problem of the file name) >> >> >> I have the other code before this as you suggested to use i+=3 instead of >> 2, >> as first code as below: >> >> //Get the directory >> dir = getDirectory("Choose a Directory "); >> SaveDir = getDirectory("Choose a Sort Save Directory "); >> >> //Create file list >> list = getFileList(dir); >> print("number of files in the Directory ="+ list.length); >> >> //Look throug the list - and move the files to >> for (i=0; i<list.length; i+=3 ) { >> if (endsWith(list[i], "/")){ >> //listFiles(""+SubDirNames[0]+list[i]); >> }else{ >> //Split the Name by "." and look at the first Entry >> Data = split(list[i],"."); >> >> // Make directory name >> DirName = Data[0]; >> >> //Ceate that directory >> File.makeDirectory(SaveDir+File.separator+ >> DirName); >> >> // Move the file to that >> Dir >> File.rename(dir + list[i],SaveDir + DirName >> + File.separator + list[i]); >> >> } >> } >> >> >> >> >> Then the second code is the one i listed as previous post: >> >> dir = getDirectory("Choose main Directory");///this contains the >> subdirectory, and tiffs >> dir2 = getDirectory("choose the subdirectory folder"); ///this contains the >> folder specific to each section >> >> >> list = getFileList(dir); >> list2 = getFileList(dir2); >> print("number of files in the Directory ="+ list.length); >> n=1; >> >> for (i=1; i<list.length; i+=2 ) { >> if (endsWith(list[i], ".tif")){ >> print(list[i]); >> for (k=0; k<list2.length; k++){ >> if (endsWith(list2[k], "/")){ >> >> >> oldpath= dir + File.separator+ list[i]; >> newpath = dir2 + File.separator+ list2[k] >> + >> File.separator+ n+".tif"; ///this renames files to 1 >> print(newpath); >> print(oldpath); >> >> File.rename(oldpath, newpath); >> >> } >> }}} >> >> The problem here is the file name. I cannot keep the original file name and >> move those into the associated folders. >> I appreciate any suggestion. >> >> Rui >> >> >> >> -- >> View this message in context: >> http://imagej.1557.x6.nabble.com/Select-every-other-image-and-move-into-specific-folder-tp5005416p5005423.html >> Sent from the ImageJ mailing list archive at Nabble.com. >> >> -- >> ImageJ mailing list: http://imagej.nih.gov/ij/list.html >> > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by George Patterson
hi,
Thank you so much for your help! The code you wrote worked perfectly. but I am not clear how the last three lines (Data2=findDataSet, else, File.rename...) can work the three sets of images into one folder. Rui |
Hi Rui,
Glad to hear it worked for you. In that macro I intended variable "Data2" to designate each data set of three images. The variable "findDataSet" is a substring of each filename in which the 001, 002, or 003 are removed. The if(findDataSet!=Data2) is to check if the filename belongs to a new data set, "E8_002" for example. If not, a new directory is made with that filename and the file is moved to that directory. The Data2=findDataSet just before the }else{ sets the "Data2" variable to the base filename designating that data set, "E8_002". So now, the next time through the for loop if the next file is E8_002_002, the findDataSet substring would again be "E8_002". Thus a new directory would not be made and the file would simply be moved into the directory made in the previous loop. Now that I look at it again, I suggest one small change. It may never make any difference but it shouldn't hurt either. Change the }else{ to } else if (findDataSet==Data2){ As currently written it will move any files into that folder. If you've other files with different names, those might all get moved into one of your data set folders. You might even add another bit to check that the files are images since these are probably the only ones you want to move. For instance, you could change if (endsWith(list[i], "/")){ //listFiles(""+SubDirNames[0]+list[i]); }else{ to if (endsWith(list[i], ".tif")){ and get rid of the else part of the loop. On Mon, Nov 4, 2013 at 1:48 PM, rtas <[hidden email]> wrote: > hi, > > Thank you so much for your help! > The code you wrote worked perfectly. but I am not clear how the last three > lines (Data2=findDataSet, else, File.rename...) can work the three sets of > images into one folder. > > > Rui > > > > > -- > View this message in context: > http://imagej.1557.x6.nabble.com/Select-every-other-image-and-move-into-specific-folder-tp5005416p5005444.html > Sent from the ImageJ mailing list archive at Nabble.com. > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |