Match files by name?

I have one folder of files which are
and another folder that has corresponding files in it named
“2222.jpg”,“023333.jpg”,“4b5555.jpg” etc.
How could I rename all files in folder 1 to be identical to folder 2?
In other words, if there is a file named “01a2222.jpg” in folder 1 I would
like it to find file 2222.jpg (matched by 4 characters before comma) in
folder 2 and rename it"01a2222.jpg".

I started trying with something like this:

tell application “Finder”
set the name of (files of folder “folder1” whose name text -8 through -4 is
“4444.”) to “02a4444.”
end tell

I don’t have the slightest clue why this does not work. It should. I know
you can refer to a file name with “text”. Strange.

Anyway. Any suggestions are very much appreciated