Login  Register

Re: Array of strings- a macro question

Posted by GuyM on Aug 05, 2015; 6:47pm
URL: http://imagej.273.s1.nabble.com/Array-of-strings-a-macro-question-tp5013893p5013911.html

Hello George,
Thanks for the code and for the assistance; for the meantime I've managed to work around this issue by using indexOf: when its value is not -1, this is indication that there is a match between regexp and string, and the file is saved into the appropriate subfolder.

I've appended below the code I wrote, for the sake of the greater mailing list community. Feel free to comment and change as needed.

The code is intended for the situation where many files of different properties/modalities (i.e. channels, timepoints etc) are found in a single folder and the user would like to separate some/all of them into subfolders, based on unique string sequences that appear in their names. There is no limit on the number of differentiating strings. It is currently set to work on tiff files.
All the best
Guy

path = getDirectory("Choose a directory");
filename = getFileList(path);
MyString=getNumber("Into how many different subfolders would you like to sort the files?", 2);

Dialog.create("Enter names of folders into which files will be subdivided");
Dialog.addMessage("Enter strings as names of folders into which files will be subdivided. \n Names are based on different strings (eg channels, timepoints) that appear in the files names.");
title="None";

for (j=0; j<MyString; j++) {
Dialog.addString("String "+j, title);
}

Dialog.show();

StringArray=newArray(MyString);
FolderListPath=newArray(MyString);
for (j=0; j<MyString; j++) {
CurrentString= Dialog.getString();                          

StringArray[j]=CurrentString;
CurrentPath = path +CurrentString+ File.separator;
FolderListPath[j]=CurrentPath;

File.makeDirectory(CurrentPath);
}

setBatchMode(true);

for (i=0; i<filename.length; i++) {
        if(endsWith(filename[i], ".tif")) {
                open(path+filename[i]);
name=File.nameWithoutExtension;

for (checkIndex=0;checkIndex<MyString; checkIndex++) {

indexOfCh=indexOf(name, StringArray[checkIndex]);
if (indexOfCh != -1) {
saveAs("tiff", FolderListPath[checkIndex] + getTitle);
}
}
close();
        }
}

"DONE"

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html