Hi, all
I have a main folder that includes the subfolder, and within the subfolder there are few sub-sub-folders. Basically, the order of folder is that "folder_test/Slide XX/section #" and within the section # folder there are few images. I have many folders with XX and # which are sequential numerical numbers. What I want to do is to rename the name of the last folder(section #) to be sXX_sec##. Then I would like to open the jpeg images within the original folder(section #) to save the image as sXX_sec##_01[last digits would be sequential] within the newly made folder called sXX_sec##. For example, if i have folder test/Slide 34/section 2, I want to rename jpge images within the section 2 folder as s34_sec2_01, s34_sec2_02 so on , within the newly made folder, s34_sec2. I don't seem to find a easy way to rename a folder name since I am really a beginner of macro, so now I am trying to extract the name from the original folder name and find a way to use this to make a name of new folder. The problem now is that if i use "input" as a name of directory, the primary folder ("folder_test") is returned at the end in the log. I am not sure substring to change the name of folder would work. input = getDirectory("Input directory"); suffix = ".JPG"; //you can only apply function below for JPEG images processFolder(input); function processFolder(input) { list = getFileList(input); for (k = 0; k < list.length; k++) { if(File.isDirectory(input + list[k])) //if it's a directory, go to subfolder processFolder("" + input + list[k]); if(endsWith(list[k], suffix)) //if it's a tiff image, process it print (input + list[k] + "is a path"); //if it's neither a tiff nor a directory, do nothing file1 = list[k]; print (input + "is a directory"); print (list[k] + "is a file name"); ///listing all folder name with ///here rename image name function need/ } } Simply is there any easy way to rename a folder based on extracting some part of the current name of folder (e.g. some text and numbers)? Thank you in advance! RT |
Good day RT,
you asked: "is there any easy way to rename a folder based on extracting some part of the current name of folder" Yes. This example macro should get you started... // folders: test/Slide 34/section_2 // file name: s34_sec2_01.jpg etc. ///////////////////////////////////// subsubDirPath = getDirectory( "Select subsub-directory" ); a = split( subsubDirPath, "/" ); subsubDir = a[a.length-1]; subDirPath = File.getParent( subsubDirPath ); a = split( subDirPath, "/" ); subDir = a[a.length-1]; subsubNum = substring( subsubDir, lengthOf(subsubDir)-1 ); subNum = substring( subDir, lengthOf(subDir)-2 ); folderName = "s"+ subNum + "_sec" + subsubNum; File.makeDirectory( subsubDirPath + folderName ); i = 1; fileName = folderName + "_" + IJ.pad(1, 2) + ".jpg"; print( "FileName: " + fileName ); ///////////////////////////////////// Best Herbie :::::::::::::::::::::::::::::::::: Am 07.10.16 um 18:41 schrieb rtas: > Hi, all > > I have a main folder that includes the subfolder, and within the subfolder > there are few sub-sub-folders. > Basically, the order of folder is that "folder_test/Slide XX/section #" and > within the section # folder there are few images. > I have many folders with XX and # which are sequential numerical numbers. > What I want to do is to rename the name of the last folder(section #) to be > sXX_sec##. > Then I would like to open the jpeg images within the original folder(section > #) to save the image as sXX_sec##_01[last digits would be sequential] within > the newly made folder called sXX_sec##. > > For example, > if i have folder test/Slide 34/section 2, I want to rename jpge images > within the section 2 folder as s34_sec2_01, s34_sec2_02 so on , within the > newly made folder, s34_sec2. > > I don't seem to find a easy way to rename a folder name since I am really a > beginner of macro, so now I am trying to extract the name from the original > folder name and find a way to use this to make a name of new folder. The > problem now is that if i use "input" as a name of directory, the primary > folder ("folder_test") is returned at the end in the log. I am not sure > substring to change the name of folder would work. > > input = getDirectory("Input directory"); > > suffix = ".JPG"; //you can only apply function below for JPEG images > > processFolder(input); > > function processFolder(input) { > list = getFileList(input); > for (k = 0; k < list.length; k++) { > if(File.isDirectory(input + list[k])) //if it's a directory, go to > subfolder > processFolder("" + input + list[k]); > if(endsWith(list[k], suffix)) //if it's a tiff image, process it > print (input + list[k] + "is a path"); //if it's neither a > tiff nor a directory, do nothing > file1 = list[k]; > print (input + "is a directory"); > print (list[k] + "is a file name"); ///listing all folder name with > ///here rename image name function need/ > } > } > > > > Simply is there any easy way to rename a folder based on extracting some > part of the current name of folder (e.g. some text and numbers)? > > Thank you in advance! > > RT > > > > > -- > View this message in context: http://imagej.1557.x6.nabble.com/rename-folder-name-tp5017317.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 |
Hi,
Thank you for prompt response! I have been tried to understand the code that you kindly made. However, although I chose the subsubfolder as section##, for example section 2, the code returned the new file name "s34_sec" and no number after the "sec". so i tried and error to change the "subsubNum" in the code. I used lengthOf (subsubDir)-2 instead of lengthOf (subsubDir)-1. Although this works (see below), I am not quite sure what lengthOf (subsubDir)-2 is getting. Does this mean returning the last two index of name of subsubDir folder? Then should not it return the "(space)2" instead of 2? I also modified the last part of code, adding the cropping and renaming the jpeg file. Here is the revised code : subsubDirPath = getDirectory( "Select subsub-directory" ); a = split( subsubDirPath, "/" ); subsubDir = a[a.length-1]; subDirPath = File.getParent( subsubDirPath ); a = split( subDirPath, "/" ); subDir = a[a.length-1]; print (subDir + "is a subDir" ); print (subsubDir + "is a subsubDir"); subsubNum = substring( subsubDir,lengthOf(subsubDir)-2);//this changed to 2 instead of "1" subNum = substring( subDir, lengthOf(subDir)-2); folderName = "s"+ subNum + "_sec" + subsubNum; File.makeDirectory( subsubDirPath + folderName ); print(folderName + "is a folderName"); output = getDirectory ("select save folder"); print (output); n = 1; list = getFileList(subsubDir); for (i=0; i<list.length; i++) { if (endsWith(toLowerCase(list[i]),".jpg")){ open(subsubDir + list[i]); print (list[i]); makeRectangle(780, 0, 3690, 3456); run("Crop"); fileName = substring (folderName, 0, lengthOf(folderName)-1); ///fileName (removing "/"as a filename from foldername) print (fileName, "is a file name"); saveAs("jpg", output+fileName+"_"+n+".jpg"); //manual selection of saving folder and somehow the image is 90 degree turned close(); n++; } } Thank you in advance, rui |
Dear rui,
my code works perfectly here. It creates a folder named "s34_sec2" in subsub-folder "section_2" and a file name "s34_sec2_01.jpg" and this is what you were aiming at -- no? Perhaps you are on a windows machine and need to alter the separator. On a Mac it is "/". lengthOf( str ) gives you the number of chars in a string. It is described here: <https://imagej.nih.gov/ij/developer/macro/functions.html#lengthOf> There was one minor glitch in my code. It should read: i = 1; // loop index fileName = folderName + "_" + IJ.pad( i, 2 ) + ".jpg"; Best Herbie :::::::::::::::::::::::::::::::::: Am 11.10.16 um 17:56 schrieb rtas: > Hi, > > Thank you for prompt response! I have been tried to understand the code that > you kindly made. > However, although I chose the subsubfolder as section##, for example section > 2, the code returned the new file name "s34_sec" and no number after the > "sec". so i tried and error to change the "subsubNum" in the code. I used > lengthOf (subsubDir)-2 instead of lengthOf (subsubDir)-1. > Although this works (see below), I am not quite sure what lengthOf > (subsubDir)-2 is getting. Does this mean returning the last two index of > name of subsubDir folder? Then should not it return the "(space)2" instead > of 2? > I also modified the last part of code, adding the cropping and renaming the > jpeg file. > > Here is the revised code : > > subsubDirPath = getDirectory( "Select subsub-directory" ); > a = split( subsubDirPath, "/" ); > subsubDir = a[a.length-1]; > subDirPath = File.getParent( subsubDirPath ); > a = split( subDirPath, "/" ); > subDir = a[a.length-1]; > print (subDir + "is a subDir" ); > print (subsubDir + "is a subsubDir"); > > subsubNum = substring( subsubDir,lengthOf(subsubDir)-2);//this changed to 2 > instead of "1" > subNum = substring( subDir, lengthOf(subDir)-2); > > folderName = "s"+ subNum + "_sec" + subsubNum; > File.makeDirectory( subsubDirPath + folderName ); > print(folderName + "is a folderName"); > > output = getDirectory ("select save folder"); > print (output); > n = 1; > list = getFileList(subsubDir); > for (i=0; i<list.length; i++) { > if (endsWith(toLowerCase(list[i]),".jpg")){ > open(subsubDir + list[i]); > print (list[i]); > makeRectangle(780, 0, 3690, 3456); > run("Crop"); > fileName = substring (folderName, 0, lengthOf(folderName)-1); > ///fileName (removing "/"as a filename from foldername) > print (fileName, "is a file name"); > saveAs("jpg", output+fileName+"_"+n+".jpg"); //manual selection > of saving folder and somehow the image is 90 degree turned > close(); > n++; } > > } > > Thank you in advance, > > rui -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |