How to save/or move files into newly directories



I tried to create folders which use the filename as folders’ names. Next, I would like to put those images with the same filename into the specific folder but those files stay on the original directory. Is there a way to complete the work? Thanks in advance.

Please have a look at my macro,

dir = getDirectory("Images to process"); 
lista = getFileList(dir);
  for (i=0; i<lista.length; i++) {
  run("Close All"); 
  inputPath = dir + lista[i]; 
run("Bio-Formats Importer", "open=[" + inputPath + "]  autoscale color_mode=Default view=Hyperstack stack_order=XYCZT");

results_Dir = dir + imgName;

rename("test");//This macro is designed for Blue-green-red or Blue-red-green. If the channel's order is different, you can add a line "run("Arrange Channels...", "new=213 or else");" to change the order. 

run("Split Channels");
run("Duplicate...", " ");
run("Gaussian Blur...", "sigma=5");
imageCalculator("Subtract", "C3-test","C3-test-1");

run("Duplicate...", " ");
run("Gaussian Blur...", "sigma=5");
imageCalculator("Subtract", "C2-test","C2-test-1");

run("Merge Channels...", "c1=C3-test c2=C2-test");
run("RG2B Colocalization", "use minimum=0 red=0 green=0 set=[the max of the red and green] display=[an 8bit image]");

selectWindow("Colocalization Data - RGB");



Keep an eye on your file and path separators. getDirectory() provides a string terminated with a path separator (at least on Windows), but when you create results_Dir you don’t add one to the end of your path string.

This means that your saveAs path is lacking a separator between the path name and the file name. A (possible) simple fix is to replace the line above with:


I’ve not run your code, but maybe give it a try and see if it fixes your problem.


Thank you dnmason for your suggestion. It works.

From the beginning, I did not use ‘dir2=getDirectory(“Choose Output Folder”)’ because I thought that these result files were able to be saved into those relative created directories. I also read some macro templates which use ‘File.separator’ but have no idea which “timing” is necessary to put a separator.
The function: File.separator - Returns the file name separator character ("/" or “”) seems ambiguous to me. And you said that it needs a separator between the path name and the file name for _saveAs__path. Could you explain it more? Thanks again.


Glad it’s working. I’ll try to explain File.separator as best as I can. Apologies if this is too simple.

The location of a file on a file system can usually be described relative to some root point. On *NIX / Mac systems, this is called the root, so the location of a user’s home directory might be:


and a file in that directory might be:


The File separator is simply the character that separates parts of the location. In this case it’s a forward slash: “/”

On a windows system, the location is slightly different and the same example might be:


Note in this case for reasons unknown (to me) Microsoft decided to use a backslash as their File Separator “\”.

The nice thing about the ImageJ File.separator function is that it will use the native file separator for the system on which you’re running the code so your script will run correctly on any platform regardless of the file separator your system uses (the alternative if hard-coding your paths with the expectation that everyone will use a Mac or Windows machine).

The important thing is just to remember to add a file separator when you’re constructing a path to a file. If you forget, you might end up, for example, with a path that looks like this:


…which is a perfectly valid path, but you probably did not want to end up writing out a file called dnmasonmyfile.txt


Thank you for lightening solution I have been mired in the syntax.

The file separator “/” used on mac and “” used on win system is a general sense. I have never considered that it is essential for writing a macro, though, I understood it would be a line like [ saveAs(“Tiff”, “C:/Users/chin/Desktop/oib/test-1.tif”);] according to the macro recorder, but I’ve all the time confused about making result files put into the sub-directories. It’s great for me to figure out the crux of the problem through your explanation.

I appreciate your kindly explanation in detail.