move files to a folder based on its file name

OK, this should be simple…but
I have taken a bunch of .mov files and made tiff sequences of them.
This means I have in one directory a hole bunch of tiff files number sequentially.
the file names are [shot name] [white space] [sequential 3 digit number] .tiff
I want to make a folder [shot name] and group all of the tiff files for that shot into the folder.
I have been googling all day and I can’t quite get there.
this is what I have so far.



set AppleScript's text item delimiters to {}
set shot_uid_memory to ""

set all_tiff_folder to (choose folder with prompt "select folder with all tiff files in it")
set all_tiff_folder_list to (list folder (all_tiff_folder) without invisibles) as list

set AppleScript's text item delimiters to " " --so I can deliminate between [shot name] and the [sequential counter]

repeat with i in all_tiff_folder_list
	set ii to text items of i
	set shot_uid to item 1 of ii --get the [shot name]
	if shot_uid is not equal to shot_uid_memory then --ie if its changed to a new shot then make a folder to put the shots in
		tell application "Finder"
			make new folder at all_tiff_folder with properties {name:shot_uid} --make the folder to contain the tiff files that relate to this shot
			--this is where I get stuck. I want to move the current tiff file to the folder it relates to
		end tell
	else
		tell application "Finder"
			--this is where I get stuck. I want to move the current tiff file to the folder it relates to
		end tell
	end if
	
	set shot_uid_memory to shot_uid
end repeat
set AppleScript's text item delimiters to {}

Thanks in advance

Hi,

the shell command ditto is able to copy a file and create intermediate directories if needed at the same time.
As ditto does copy (not move) the file it’s necessary to add a rm (remove) command to delete the file

Sorry about choosing more descriptive variable names


set TIFFolder to (choose folder with prompt "select folder with all tiff files in it")
tell application "Finder" to set TIFFiles to files of TIFFolder whose name extension is "tiff"

set {TID, text item delimiters} to {text item delimiters, space} --so I can deliminate between [shot name] and the [sequential counter]

repeat with aFile in TIFFiles
	set fileName to name of aFile
	set shotName to first text item of fileName
	set sourcePath to quoted form of POSIX path of (aFile as text)
	set destinationPath to quoted form of (POSIX path of TIFFolder & shotName & "/" & fileName)
	do shell script "/usr/bin/ditto " & sourcePath & space & destinationPath & "; /bin/rm " & sourcePath
end repeat
set text item delimiters to TID


Hi. I started looking at this before I saw Stefan’s reply, so here is an alternate method from which to learn.

set traversalPast to {}
set folderTarget to choose folder
set nameList to (list folder (folderTarget) without invisibles)

tell application "Finder" to repeat with filename in nameList
	set theDeterminant to filename's word 1---TIDs unnecessary with "word"
	if traversalPast does not contain theDeterminant then
		move (folder folderTarget's files whose name begins with theDeterminant) to ¬
			make folder with properties {name:theDeterminant} at folderTarget
		set traversalPast's end to theDeterminant
	end if
end repeat

so so grateful.
once again…you guys rock