Create Or Rename Folders based on File or File Name

By noting exactly what you do and mirroring that behaviour in your actions.

Lets assume you have the files selected in the Finder.

See: Working with the Finder Selection for how to process the files.

You can use the Split Path action to get the extension or the Path condition to determine if it is a folder (or assume files without extensions are folders if you know they will always have an extension otherwise).

You can remember the FF number in a permanently stored variable (a Keyboard Maestro convention is to name variables like that starting with "DND" (do not delete)). eq “DND FF Index”, and you can increment that, or you can check which FF folders exist and use the next one.

What is the parent folder of the FF folders? What happens to the FF folders afterwards (you said there was at most 4, so when do they go away?).

It's pretty easy to create a new folder, create all your sub folders, and rename the selected files into the selected folders based on their extension.

You're a bit vague about what happens to folders - and vague is the enemy of robust macros. You need to be very sure you know exactly what happens when.

However you can build your macro to work with the current case, and then extend it with other cases - use the Assert action or Cancel This Macro action to detect cases you don't handle and that will alert you to problems.

Also - since you are working on files - make sure you have good backups, and ideally that you are working on copies of the files. If anything goes wrong you want to be able to fix things and retry the macro which will be difficult if you don't have a clean copy of the original, and/or a clean copy of the target.