Define output folder: Scriptable save HDF5 (new or replace) imagej macro

I am trying to create a simple macro to convert lif files to HDF5. I have problems with getting the saved images to my output folder.
Here is the script:


dir = getDirectory("Choose input Directory ");
output = getDirectory("Choose output Directory ");
print(dir);
setBatchMode(true);
   count = 0;
   countFiles(dir);
   n = 0;
   processFiles(dir);
   print(count+" files processed");
   
   function countFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              countFiles(""+dir+list[i]);
          else
              count++;
      }
  }

   function processFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              processFiles(""+dir+list[i]);
              
          else {
             showProgress(n++, count);
             path = dir+list[i];
             processFile(path);
          }
      }
  }

  function processFile(path) {
       if (endsWith(path, ".lif")) {
        run("Bio-Formats Importer", "open=[path] autoscale color_mode=Default view=Hyperstack stack_order=XYCZT");
        imgName=File.getName(path); 
        baseNameEnd=indexOf(imgName, ".lif"); 
        baseName=substring(imgName, 0, baseNameEnd);
        finalName= output + baseName +".h5";
        print(finalName);
        run("Scriptable save HDF5 (new or replace)...", "save="+finalName);
        print("Saving to: " + output);
     }
	}

If I leave the output file naming out, the files are saved in the Fiji app folder with the correct name. However, if I add the output to the saving path, the file names become H5 (keeps overwriting) in the main directory.

@ctrueden
Could you possibly help me with this?
Thank you so much

Hi @OmarMossad,

Is the printing of

correct ?
Don’t you need a File.Separator ?
Is your imgName correct ? (check it with a print)

Nico

1 Like

with the current script it prints the full path to the file. but if I remove the output part like the following it saves the correct files but to fiji app folder. I don’t know to how direct it somewhere else.

function processFile(path) {
       if (endsWith(path, ".lif")) {
        run("Bio-Formats Importer", "open=[path] autoscale color_mode=Default view=Hyperstack stack_order=XYCZT");
        imgName=File.getName(path);
        print(imgName);
        baseNameEnd=indexOf(imgName, ".lif"); 
        baseName=substring(imgName, 0, baseNameEnd);
        finalName= baseName +".h5";
        print(finalName);
        run("Scriptable save HDF5 (new or replace)...", "save="+finalName);
        print("Saving to: " + output);

the imgName is correct. filename.lif

@OmarMossad Can you share the output? What file paths get printed? Note that if your file path has spaces, you need to enclose the macro argument in square brackets, like so:

run("Scriptable save HDF5 (new or replace)...", "save=["+finalName+"]");

Otherwise, the file path will be interpreted as multiple arguments to the command.

1 Like

That actually worked quite well. It was enclosing the argument as you mentioned. Thank you so much for your time @ctrueden!

1 Like