How to extract folder name

Hi all,
I’m manipulating files taken in Volocity using ImageJ (Fiji) and the name of the folder is automatically incorporated into the filename when I open them in Bio-Formats Importer using a macro. E.g. a file could be called “MyExpriment.mvd2 - MyImage1”
I would like my macro to exclude this beginning "MyExperiment.mvd2 - " WITHOUT having to manually write in each macro what I called my expreriment that day. For this I need to extract the name of the folder the images are stored in, but only the folder name, not the entire path. Any suggestions of how I could do that? A simple substraction of strings doesn’t work, and in order to use replace(string, old, new), I need to escape every metacharacter, e.g. slash, in the path.

function processImage(path, series) {
IJ.log("Processing " + series + “…”);
run(“Bio-Formats Importer”,
“open=[” + path + "] " +
"autoscale " +
"color_mode=Grayscale " +
"view=Hyperstack " +
"stack_order=XYCZT " +
“series_” + series);
folderpath = File.getParent(path);
folderparent = File.getParent(folderpath);
folder = replace(folderpath, folderparent, “”); //this doesn’t work
title = getTitle();
title = replace(title, folder, “”);
title = replace(title, ".mvd2 - ", “”);

// actual image processing goes here


Any sugestions welcome!

Hi @MarFra
I wrote a script sometime back, may be this could help:


1 Like

Thanks Ved! It’s a useful workaround once the files have been named wrong.
Still, I dream about not having to think about changing named at all and just have a step that automatizes it in every macro I run. :wink: