Changing file names based on array/matrix position

Hi all,

I’m pretty new to FIJI/ImageJ and am just starting out writing macros, so hopefully this is an easy question. I have a file directory where the tif images are named in a specific order, and want to rename them such that two numbers from the original filename are converted to another two numbers in the final filename. There’s no mathematical operation for the conversion, but it is structured and predictable (i.e. we have a key), so we’re currently using a series of if/else statements to rename the files as we iterate through the files in the folder.

Here’s an example for the first three if/else statements, plus the code beforehand for a bit of context. This is for 36 fields, so there are 36 if/else statements total.

for (fieldCounter = 0; fieldCounter < 36; fieldCounter++) //for loop to iterate through finalFieldArray
currentMoveField = finalFieldArray[fieldCounter]; //sets current field being moved to temporary stitch directory
print("Current field being renamed to tile form: " + currentMoveField); //print statement to print current field being renamed
oldPath2 = finalMergePath + “/” + currentMoveField; //creates oldPath2 to hold old location of currentMoveField
oldFieldStartIndex = indexOf(currentMoveField, “f”) + 1; //finds starting index of currentMoveField
oldField = substring(currentMoveField, oldFieldStartIndex, oldFieldStartIndex + 2); //takes substring of currentMoveField to find oldField
print("Field before rename: " + oldField); //print statement to print old field
if (oldField == “00”) //series of if/else if statements to rename currentMoveField
newField = “21”;
else if (oldField == “01”)
newField = “22”;
else if (oldField == “02”)
newField = “16”;

It continues this way for 36 rounds (defined by the fieldCounter), until oldField =“35”, then repeats 24 times for all of the files in the folder. The code works fine for now, but we want to expand to 169 fields, which would require 169 if/then statements, which is not only tedious but also seems inefficient.

Since the files are in a predictable order (and we’re tracking where we are within the folder using the fieldCounter), is it possible to code it so that we’re using arrays, e.g.

oldFieldArray = {“00”, “01”, “02”, “03”, “04”, “05”, “06”, “07”, […], “168”, “169”}
newFieldArray = {“85”, “86”, “73”, “72”, “71”, “84”, “97”, "98’, […], “168”, “169”}

so that if fieldCounter=1, it looks at the first position in oldFieldArray, checks that the name of the file has oldField=“00” and if that’s true, changes the value to the first position in newFieldArray, i.e. “85”, and so on for the second position,etc., until the fieldCounter maxes out. As a failsafe, if oldField=“00” is false, then search through the array until it finds a match (say position 5, “04”), then inserts the corresponding value from newFieldArray (“71”)?

Please let me know if this makes sense, or if you need more context. I apologize ahead of time for any incorrect nomenclature, and appreciate any help. Thanks!


Would mentioning of the replace to combine with the indexOf function suffice?
Or do you also wish for the getFileList(inPath) function (which already holds the directorySeparator character, by the way, so +"/"+ can be left out in the file path construction) so you can get all the tif file names to check for the position(s) of the oldField string.
I notice that your oldFieldArray is strictly consecutively incremental.
Therefore, you can generate the oldFileldArray[i] string by the function IJ.pad(i,2) instead and do away with that oldFieldArray.

You can recursively parse the filename for oldField where you disregard the left of the string that was already recognised, replacing along the way: file_00_00_01 would be parsed first for file_00, then _00_01 would be parsed for _00, then _01 would be parsed, which ends the recursion.