Copy files from multiple directories based on file name

I took a stab at this and this is what I came up with:

set startFolder to (choose folder with prompt "Choose a folder of images you want to move to the server.") as text
tell application "Finder" to set fileList to name of every file in folder startFolder

repeat with fileName in fileList
	--Setting the destination path up. 
	set rootPath to "/Users/local/BRS/nhill/Documents/Applescripts/Send Image Script/FolderTest/Server/"
	tell fileName to set {fileGS, folderA, folderB} to {text 10 thru 11, text 1 thru 2, text 3 thru 4}
	if fileGS contains "gs" then set rootPath to "/Users/local/BRS/nhill/Documents/Applescripts/Send Image Script/FolderTest/server lineart/"
	--Set the full path of current file to be copied
	tell fileName to set deleteName to (text 1 thru 9)
	--Delete existing images
	set deleteFolder to rootPath & folderA & "/" & folderB & "/"
	tell application "Finder" to delete (every item of folder (deleteFolder) whose name begins with deleteName)
	--Do the copy stuff
	set sourceFile to quoted form of (POSIX path of startFolder & fileName)
	set destinationPath to quoted form of (POSIX path of rootPath & folderA & "/" & folderB & "/" & fileName)
	do shell script "/usr/bin/ditto " & sourceFile & space & destinationPath
	tell application "Finder" to set label index of file (startFolder & fileName) to 6
end repeat

But i’m getting this error:
error “Finder got an error: Can’t get folder "/Users/local/BRS/nhill/Documents/Applescripts/Send Image Script/FolderTest/Server/54/32/".” number -1728 from folder “/Users/local/BRS/nhill/Documents/Applescripts/Send Image Script/FolderTest/Server/54/32/”

Regarding the delete operation: Ditto overwrites any existing file with the same name. Can you delete all files in the destination folder before copying the new ones?

This is a slightly optimized version of the script which just checks for the string “gs.” (with trailing dot) for the GS version and uses a list of path components which are concatenated with text item delimiters to a POSIX path

property rootPathComponents : {"", "Volumes", "graphics", "images"} -- later becomes "/Volumes/graphics/images"

set startFolder to (choose folder with prompt "Choose a folder of images you want to move to the server.") as text
tell application "Finder" to set fileList to name of every file in folder startFolder

repeat with fileName in fileList
	--Setting the destination path up. 
	tell fileName to set {folderA, folderB} to {text 1 thru 2, text 3 thru 4}
	if fileName contains "gs." then
		set colorFolder to "Lineart"
	else
		set colorFolder to "color"
	end if
	--Set the full path of current file to be copied
	set sourceFile to quoted form of (POSIX path of startFolder & fileName)
	set pathComponents to rootPathComponents & {colorFolder, folderA, folderB, contents of fileName}
	set {TID, text item delimiters} to {text item delimiters, "/"}
	set destinationPath to quoted form of (pathComponents as text)
	set text item delimiters to TID
	do shell script "/usr/bin/ditto " & sourceFile & space & destinationPath
	tell application "Finder" to set label index of file (startFolder & fileName) to 6
end repeat

I cannot delete all files in that folder :/. The ditto replacing any images with the same name would work fine except for some of the older files being jpg or pngs. so they don’t get replaced by the tif file.

Using a routine to check every time for older files is not very efficient.
Consider to use a second script (once) to go thru the folders and clean up all non-tif files.

But I won’t always have a new tif file image for those that are still jpg and png.
I’m really just replacing them as I get them. Only for the ones that i’m using this script for.

because what’s happening is that we get new images from vendors for products new and old. The images use to just get dumped into our image library with no processing or quality control. Now we process them into tifs and edit any images that need it. So we are slowly replacing the old with new ones as we get them.

This deletes every file in the destination folder which starts with the base name (without file extension) before copying the new file

property rootPathComponents :  {"", "Volumes", "graphics", "images"}

set startFolder to (choose folder with prompt "Choose a folder of images you want to move to the server.") as text
tell application "Finder" to set fileList to every file in folder startFolder whose name extension is "tif"

repeat with aFile in fileList
	--Setting the destination path up. 
	set {name:fileName, name extension:fileExtension} to aFile
	set baseName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
	tell fileName to set {folderA, folderB} to {text 1 thru 2, text 3 thru 4}
	if baseName ends with "gs" then
		set colorFolder to "Lineart"
	else
		set colorFolder to "color"
	end if
	--Set the full path of current file to be copied
	set sourceFile to quoted form of (POSIX path of startFolder & fileName)
	set pathComponents to rootPathComponents & {colorFolder, folderA, folderB, ""}
	set {TID, text item delimiters} to {text item delimiters, "/"}
	set destinationPath to pathComponents as text
	set text item delimiters to TID
	do shell script "rm " & quoted form of (destinationPath & baseName & ".") & "*"
	do shell script "/usr/bin/ditto " & sourceFile & space & quoted form of (destinationPath & fileName)
	tell application "Finder" to set label index of file (startFolder & fileName) to 6
end repeat

Awesome thanks!
One issue though, the line that deletes existing images works, as long as there are images to delete. if an image isn’t there then it errors out.

Wrap the line in a try block to ignore the error.

try
	do shell script "rm " & quoted form of (destinationPath & baseName & ".") & "*"
end try

Dude, That did it. You’re the best.