Folder action to move contents based on filename

I am trying to create a folder action that will move files to various locations depending a part on the filename. Sounds simple but I need to include a wildcard too.
The filenames are structured like this: ‘xx_xxx_00C_xxxxxx_xx’, where ‘00C’ appears is what I need the script to read. If its ‘C’ then move to folder ‘C’. If the ‘C’ is a ‘D’ move to folder ‘D’, problem is the 00 is different on all filenames. It could be anything from 01 through to 53 so I’d like that to be the wildcard. I was thinking ‘**C’ to move to folder ‘C’.

I have been trying to piece together scripts to make this happen, but I don’t have the experience or knowledge. Can anyone help?

so the numbers are always going to be 01 thru 53 right? Are the letters only going to be C or D? Or are other options available as well?

Finally you said the ##X can appear in different places in the file name, but will it always be preceded and followed by an ‘_’? Alternatively can you be assured that the combination of 01 thru 53 followed by C or D will not ever appear elsewhere at any point in the file name? (Aside of course from the portion we want to pull it from)

Yes the numbers will only range from 01 to 53 (week numbers), it will always have a ‘_’ either side, I need to include that as some jobs are A, B, or C versions which appears after the second set of xxx’s.

Oh yes and the ‘C’ could be a ‘PCW’ or a ‘CD’ or some other initial depending on the department the file belongs too.

Thank you for the reply!

Give this a try

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set {tid, text item delimiters} to {text item delimiters, "_"}
		repeat with aFile in these_items
			set aName to name of aFile
			set {jc1, jc2, wc, pc, asize, res} to text items of fileName
			set chain to text 3 thru -1 of wc
			(*
				move your file now to the correct server path
				chain contains the character portion of your week & chain section of the file name
			*)
		end repeat
		set text item delimiters to tid
	end tell
end adding folder items to

Just wanted to acknowledge that James sorted this out for me. Here is the script in case anyone else need something similar!

on adding folder items to this_folder after receiving these_items
	set {tid, text item delimiters} to {text item delimiters, "_"}
	tell application "Finder"
		repeat with aFile in these_items
			set fileName to name of aFile
			-- fileName is now equal to 06_005_05C_904746_RP_SPG_LR.jpg
			set {jc1, jc2, wc} to text items of fileName
			-- jc1 = 06
			-- jc2 = 005
			-- wc = 05C
			set chain to text 3 thru -1 of wc
			-- chain = C
			-- If the Week Chain had been 51PDC  then chain would be equal to PDC
			try
				if chain = "C" then
					move aFile to folder "Your_Location" with replacing
					tell application "Finder" to delete aFile
				else if chain = "PCW" then
					move aFile to folder "Your_Location" with replacing
					tell application "Finder" to delete aFile
				end if
			end try
		end repeat
	end tell
	set text item delimiters to tid
end adding folder items to