Renaming an Image like the parent folder

Hello World,

this is my first post and I hope, you can help me with this issue. I´m pretty new to using macros in ImageJ/Fiji, but tried some things out. My problem is the following:

I have multiple folders with different names (e.g. “318_1”, “318_2”), but each with 4 pictures in it with the same names (“CH1”, “CH2”, “CH3”, “CH4”). Now I want to duplicate and rename every picture so they have the name of the parent folder + their own name and save them to a new folder on my desktop. Because I have around 150 folders, I want to achieve this by using a macro. I know there are commands like File.getParent, but I just don´t understand how I have to use them.

What I tried until now (not really great, I know :see_no_evil: )

fileName = getTitle();
dir = File.getParent(fileName);
run("Duplicate...", "dir + fileName");
run("16-bit");
saveAs("BMP", path);

Please help me! :sweat_smile:

Edited to make the code, well, code.
You should start with both the Plugins-Macros-Record so you can see what the program is doing when it duplicates an image:
For example:
run("Duplicate...", "title=Test");
is what I get when I run a duplicate with Test typed into the text box.

It also helps to add print statements to your macro to check that you have what you think you have.

fileName = getTitle();
dir = File.getParent(fileName);

print(fileName);
print(dir);

Will show that “dir” is nonsensical. The important bit there is that getTitle does not give you the path, just the title of the image (which may also be the file name, but that is also NOT what you pass to getParent). You cannot get the parent of a string of characters.

More info on the file functions here.

Additional information.
https://imagej.nih.gov/ij/macros/GetDirectoryDemo.txt

Here is a few points that will provide you some direction. Its is not a complete solution.

1.File.getParent(path) requires path as input

The line dir = File.getParent(fileName); will not produce the desired effect because it doesn’t return the parent directory. The documentation requires that the input for File.getParent(path) to be the path for the image.

To get the path of the image you need to use imageDirector = getInfo("image.directory");

2. Use getFileList(path) got the an array of the contained file/folder names

3. Loop over the content of the parent directory (containing the files “318_1”, “318_2”)

filesInsideParent = getFileList(pathToParent);
for (i = 0; i++; i < filesInsideParent.length){
    // if the item filesInsideParent is a directory, it will have File.separator appended to its end 
   pathToChiled = pathToParent+ filesInsideParent[i];
   ... do your magic
}

I recommend that you perform the task once while recording i.e. run("Record...");, then replicate the produced steps.

I hope you find these pointers helpful

@Research_Associate @omaralamoudi
thank you for your help. I was now able to write this code, which works as a batch process (via: Process ->Batch) for single folders. Now I want to use it for folders and included subfolders. Is there a simple way to do this? I found several ideas on the internet but none of them worked.

Here is my code:

path = getInfo("image.directory");
dir = File.getParent(path);
name = File.getName(path);
title= getTitle();
run("16-bit");
saveAs("BMP", "C:/Users/cbischof/Desktop/Neuer Ordner/" + name + "_" + title + ".bmp");

Thank you for the help. I really like this forum :slight_smile:

1 Like

Plugins->New->Macro opens up the macro interface, which should have a Templates sections to show you how to iterate through folders and subfolders.
You are probably most interested in ImageJ 1.x → Batch → Process folder,
It looks like the Groovy version does recursive folders while the IJ1 version processes all files in a given folder.

@Research_Associate
woohoo it works. Thank you very much. That hint about the templates was just great :ok_hand:
Thank you, that saves a lot of my time :grin:

1 Like