Macro: Is not saving in the right directory

Hey there,

It seems like i have a problem with my code.

I have an image stack of ~70-100 Images. I want to save 31 Images of the stack in a new folder called 31_Slices. (File.makeDirectory(dir+"31_Slices");
The Images are equally spread (equidistant) over the entire stack. I wrote a little loop for it, wich is working good so far.
But somehow it will not save my selected images in the right folder (31_Slices). Instead it saves them in the parent folder of it. I can’t figure out why.

Would you look at my code to help me?
This is just the loop part. The rest of the code (Dialogbox and choosing the directory) is not necessary, I think)

if (an==true) {
            n=nSlices;
            q=n/2;
            v=q/16;
            j=0;
            u=1;
            g=0;
            i=0;
            while (i<=30) {
                setSlice(u);
                run("Duplicate...", " ");
                titles=title+"_"+g;
                names = titles;
                g=g+1;
                File.makeDirectory(dir+"31_Ebenen");                            // Creates a new directory for the single images
                dir2=dir+"31_Ebenen";
                dir3 = replace(dir2, "\\","/");                                    //Changes "\" into "/" for the xml readout
                //saveAs("Tiff", dir2+names);
                run("Image Sequence... ", "format=tiff name=" + names + " save=dir3");
                close();
                j=j+v;
                u=round(j);
                i=i+1;
                }
                print(dir2);
                print(dir3);
                print(names);
        }
}

Thank you

Best Tobias

Tobias,

you could try a really minimal code yourself and I’m sure you will be able to find the problematic part. There are some unknowns (e.g. “dir”) and some superfluous definitions (q, v, j etc.) in your example code. Furthermore, you could print the various pathnames to the log-window to check if they are correct.

Best

Herbie

Hey Herby,

thanks, well I did that. You can see at the bottom of the code the print commands.
print(dir2); print(dir3); print(names);
It shows me in the log window the correct path and names of the file, but still the images are not saved in the right directory. And after trying a long time, I ran out of options…

I know my code is a bit messy wit the variables (wich I need), but I hoped that somone can point me in the right direction. I will keep looking.

Best

Tobias

This line is passing a string literal “dir3”. To get the value of the dir3 variable you need to concatenate like you did with names:

run("Image Sequence... ", "format=tiff name=" + names + " save=" + dir3);

P.S. see the macro guide for additional examples.

1 Like

Thanky hinerm,

I had tried this before, however it did not wokt either.
But after staring a while on the code and the log I learned that one / was missing.
So I added two / to the code below.
direct=dir+"/31_Slices/";
and now it’s saves in the right folder.
Sorry for the bothering, I’m still learning writing code.

Cheers

Tobias

No worries… questions are always welcome. Glad you figured it out! :smile:

1 Like