Subfolders with first 2 and then 4 characters of the file names

I am new to AppleScript and would like some help on setting up a script that can create subfolders.
What I would like is for the script to create the subfolders and name them according to the file names that are been placed in the HiRes Images folder.
Here’s my structure:

Folder: HiRes Images
Subfolders: 01 SubSubfolders: 0100, 0154, 0187, etc.
Subfolders: 02 SubSubfolders: 0200, 0209, 0290, etc.

So if I place image 015487_.eps in the HiRes folder, the script should create a subfolder 01 if it does not exist and in that 01 folder create another subsufolder 0154 if it does not exist and place image 015487_.eps in it. The script should not overwrite existing subfolders, but just sort the image.

Any help would be greatly appeciated.

Thanks in advance.
MacAttack4

Try something like this:

property folderHiRes : alias ((path to home folder as Unicode text) & "HiRes Images:")

tell application "Finder"
	launch
	set epsList to (files of folderHiRes whose name extension is "eps")
	
	repeat with thisFile in epsList
		try
			set {char4, char2} to {text 1 thru 4, text 1 thru 2} of (get name of thisFile)
			
			makeNewFolder of me at folderHiRes given name:char2
			makeNewFolder of me at result given name:char4
			
			move thisFile to result
		on error errMsg number errNum
			display dialog "Error " & errNum & return & return & errMsg buttons {"Cancel", "Skip"} default button 2
		end try
	end repeat
end tell

on makeNewFolder at _location given name:_name
	tell application "Finder"
		try
			make new folder at _location with properties {name:_name}
		on error errMsg number errNum
			if errNum = -48 then
				return ((_location as Unicode text) & _name & ":") as alias
			else
				error errMsg number errNum
			end if
		end try
	end tell
end makeNewFolder

Bruce and Jacques, thanks for your help. I went ahead and tried Jacques and it worked beautifully. I attached it as a folder action
One question I have for you Jacques is what if I don’t want to specify the extension. There might be times that I’ll use TIFF or JPG images. How could I modify the script so it’s not looking for a specific extension.

Thanks again.

Just take out these two lines:

if name extension of i is "eps" then

end if

Thanks Bruce.
I’ll give that a try.

Guys, I have one more question and maybe you can help me again. I noticed that while using Jacques Script, if a duplicate image is used, the file is not sorted, but It remains at the root of the folder. Is it possible to place the duplicates into a folder named “Duplicates”?

on adding folder items to this_folder after receiving These_items
tell application “Finder”
repeat with i in These_items
tell (get name of i) to set {t_2, t_4} to {text 1 thru 2, text 1 thru 4}
tell folder t_2 of folder this_folder
if not (exists) then make folder at this_folder with properties {name:t_2}
if not (exists folder t_4) then make folder at it with properties {name:t_4}
move i to folder t_4
end tell
end repeat
end tell
end adding folder items to

Thanks again, you guys have been a great help.
MacAttack4