Batch z project and max projection in a directory - create an array by name or file list

I have a directory with over 500 tiff files of split channels and stacks.
For each image, there are 4 channels and 5 stacks as individual tiff files (i.e., 20 files per image).
I would like to z project and then get maximum projection for each channel - i.e., for image X, select the 5 images of the same channel, z project, maximum projection, save.
I have been trying to figure out to do this based on the name.
The file names contain the stack (plane) to the left of an “-” and the channel to the right of the “-”
If I try to create a subStringArray splitting the names with split( fileName, "-"), it no longer recognizes the channel which is post “-”.

I believe another way of doing it would be to divide the directory into every 20 images as a block; take the first image, and every 4th images thereafter, to 5 images; the same for the second, third and fourth respectively.
Below is an example of a directory. X denotes the first channel of the first image, 5 planes. Y denotes the same for the second image.

r02c05f01p01-ch1sk1fk1fl1.tiff X
r02c05f01p01-ch2sk1fk1fl1.tiff
r02c05f01p01-ch3sk1fk1fl1.tiff
r02c05f01p01-ch4sk1fk1fl1.tiff
r02c05f01p02-ch1sk1fk1fl1.tiff X
r02c05f01p02-ch2sk1fk1fl1.tiff
r02c05f01p02-ch3sk1fk1fl1.tiff
r02c05f01p02-ch4sk1fk1fl1.tiff
r02c05f01p03-ch1sk1fk1fl1.tiff X
r02c05f01p03-ch2sk1fk1fl1.tiff
r02c05f01p03-ch3sk1fk1fl1.tiff
r02c05f01p03-ch4sk1fk1fl1.tiff
r02c05f01p04-ch1sk1fk1fl1.tiff X
r02c05f01p04-ch2sk1fk1fl1.tiff
r02c05f01p04-ch3sk1fk1fl1.tiff
r02c05f01p04-ch4sk1fk1fl1.tiff
r02c05f01p05-ch1sk1fk1fl1.tiff X
r02c05f01p05-ch2sk1fk1fl1.tiff
r02c05f01p05-ch3sk1fk1fl1.tiff
r02c05f01p05-ch4sk1fk1fl1.tiff
r02c05f02p01-ch1sk1fk1fl1.tiff
r02c05f02p01-ch2sk1fk1fl1.tiff
r02c05f02p01-ch3sk1fk1fl1.tiff
r02c05f02p01-ch4sk1fk1fl1.tiff
r02c05f02p02-ch1sk1fk1fl1.tiff Y
r02c05f02p02-ch2sk1fk1fl1.tiff
r02c05f02p02-ch3sk1fk1fl1.tiff
r02c05f02p02-ch4sk1fk1fl1.tiff
r02c05f02p03-ch1sk1fk1fl1.tiff Y
r02c05f02p03-ch2sk1fk1fl1.tiff
r02c05f02p03-ch3sk1fk1fl1.tiff
r02c05f02p03-ch4sk1fk1fl1.tiff
r02c05f02p04-ch1sk1fk1fl1.tiff Y
r02c05f02p04-ch2sk1fk1fl1.tiff
r02c05f02p04-ch3sk1fk1fl1.tiff
r02c05f02p04-ch4sk1fk1fl1.tiff
r02c05f02p05-ch1sk1fk1fl1.tiff Y
r02c05f02p05-ch2sk1fk1fl1.tiff
r02c05f02p05-ch3sk1fk1fl1.tiff
r02c05f02p05-ch4sk1fk1fl1.tiff

The filename seems to be well defined.
I would determine the list of unique image name as step I. Then this list can be used to create and access the plane and channels according to your needs.
Here is a pseudo code:

dir = getDirectory
imageList = getImageList

setList = new EmptyList
for ( entries in allImagesInImageList ){
	name = ExtractImageName
	if (name is not in setList)
		add name to setList
}

for ( entries in setList ){
	for (all planes 1 .. 5){
		for (all channel 1 .. 4){
			construct filename
				(i.e. entry + "p0" + plane + "-ch" + channel + "sk1fk1fl1.tiff"
			do something with file with filename
		}
	}
}

Mayby the following macro lines can also helpful:

s = "r02c05f02p01-ch1sk1fk1fl1.tiff"

a1 = split(s, "-")
for (i=0; i<a1.length; i++)
  IJ.log(a1[i]);

imgName = substring(a1[0], 0, 9);
IJ.log( imgName );

stackNum = substring(a1[0], 11, 12);
IJ.log( stackNum );

chnNum = substring(a1[1], 2, 3);
IJ.log( chnNum );