Help for (maybe simple) Folder action task highly appreciated...?

Hi everybody,

first I’d like to apologize for English as I’m not a native speaker.

And unfortunately I’m not a programmer and I have very little experience in scripting so any help with this little task is very appreciated:

I’d like to script a ‚folder action‘, that extracts the destination folder out of the name of the file and moves the file to the specific folder.

Every file that will be dropped in the folder will be named like this:

project-company_name-date-any comment (project abbrevation is always 4 digits)

i.e. cddf-apple-201223-invoice

Step 1
The script should analyze the file name and extract ‚project‘ and ‚company_name‘.
In this example ‚project‘ is cddf an ‚company_name‘ is apple.

Step 2
By using the extracted ‘project’ the script should open the matching project folder on the server named like this:

project-any comment

i.e. cddf-corporate_design_consulting

Step 3
By using the extracted ‘company_name’ the script should search each folder and subfolder recursively within this project folder to look for the existence of a document named as follows. The document serves as a kind of marker flag:

company_name-documents

i.e. apple-documents

Step 4
The script should move the file to the folder, that contains the marker flag file.
If there is already a file with the same file-name, the script should add a counter digit at the end of the name so that both file were kept with unique file-names.

If anything else goes wrong, like if some of the folders are missing or the flag file is missing, the script should move the file to a specific folder named „orphaned files“.

That’s it.
Maybe someone knows ‚parts‘ of this task or samples that just need a little adjustment so that I can put them together for my needs?

Any help will be appreciated very much,
kind regards

traegheitsmoment

Operating System: macOS 10.14