Login  Register

Re: rename folder name

Posted by Herbie on Oct 07, 2016; 5:54pm
URL: http://imagej.273.s1.nabble.com/rename-folder-name-tp5017317p5017319.html

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