Working file rename script gets stuck when seeing duplicate file name.

Hello Folks,

This is the standard file naming structure of my files. 3 letters, 4 numbers, 1 letter then usually 1 or 2 numbers, .ps
example… “AAA1111A1.ps” or “AAA1111A11.ps”. The last number represents a page number and should always be 2 digits.

So this script strips of the .ps, then checks to see if its a 1 or 2 digit page number. If it has only 1 number it will add a 0 to make it a double digit page number. "AAA1111A1.ps would end up “AAA111A01”.
After this is done the file is copied to 2 different folders.

My problem is that occasionally a file may get renamed but not moved. Resulting in “AAA1111A01” to stay in the original folder.
When this happens, the script will not process any new file that had the same name. It should just overwrite the file.
But the files just stay in limbo in the original folder.
Example
AAA1111A01
AAA1111A1.ps

Not sure how to get around this.
Any Ideas would be very appreciated.

property theExtension : “.ps”
property destinationFolder : “Panther HD:DoneCopy:”
property processedFolder : “Panther HD:processed:”

on adding folder items to thisFolder after receiving addedItems
delay 3
try
set theCount to the number of items in the addedItems

    repeat with i from 1 to theCount
        tell application "Finder"
            if size of (item i of addedItems) is 0 then
                delay 10
            end if
        end tell
        delay 1
        set aliasPath to (item i of addedItems)
        tell application "Finder"
            set theName to name of aliasPath
        end tell
       
        set theOffset to (needtoProcess(theName))
        if theOffset is not 0 then
            set newName to updateName(theName, theOffset)
           
            if newName is not "" then
                set theReturn to moveFile(aliasPath, newName)
                display dialog "File " & newName & " processed " & theReturn giving up after 2
            end if
        end if
    end repeat
on error errStr
end try

end adding folder items to

on needtoProcess(theName)
set theOffset to 0
set theReverse to (reverse of (characters of theExtension) as string)
if character -1 of theName is not “:” then --not a folder
set theOffset to offset of theReverse in (reverse of (characters of theName) as string)
end if
if theOffset is not 0 then
set theOffset to theOffset + 3
end if
return theOffset
end needtoProcess

on updateName(theName, theOffset)
set theReturn to “”
set theName to (text 1 thru -theOffset of theName)
set numList to “1234567890”
set charToTest to character -1 of theName
if charToTest is in numList then
if character -2 of theName is not in numList then
set theReturn to text 1 thru -2 of theName & “0” & charToTest
else
set theReturn to theName
end if
else
set theReturn to theName
end if
return theReturn
end updateName

on moveFile(theFile, newName)
set wasSuccess to 0
set folderString to theFile as string
set theFolder to getfolder(folderString)
with timeout of 1000 seconds
tell application “Finder”
activate
if not (exists item (theFolder & newName)) then
set name of theFile to newName
set fileAlias to ((theFolder & newName) as alias)
set newAlias to (duplicate fileAlias to folder destinationFolder with replacing) as alias
move fileAlias to folder processedFolder with replacing
set wasSuccess to 1
end if
end tell
end timeout
return wasSuccess
end moveFile

on getfolder(folderString)
set theOffset to offset of “:” in (reverse of (characters of folderString) as string)
return text 1 thru -(theOffset) of folderString
end getfolder

on makeSuredupisDone(newAlias)
set theCounter to 0
set doRepeat to true
repeat while doRepeat is true
try
set fileRef to open for access newAlias
close access fileRef
set doRepeat to false
on error
set theCounter to theCounter + 1
end try
end repeat
return theCounter
end makeSuredupisDone

The goal of the script is simple while the approach is complicated, making it hard to track what might be happenning. You could narrow down your script to find a solution. Ex:

All the handlers do not seem necessary as they are only accessed once each repeat.
SC

:smiley:
Sitcom, Wow. Thanks for the very quick response. I have a lot to learn. I did make a mistake in my description. The file is 1 character longer than I said. But your solution is so easy to read that it took seconds to fiqure out how to fix it. This is so simple.
I’m going to try to figure out the proper move commands and put it to the test tonight.

I think I see why my script was blowing up. I don’t know how to script the moving of a file properly either. Apperantly using
“with replacing” doesn’t acually replace the file as I thought it was. So after adding these lines any file with the same name
get’s renamed but not moved.

	move ThisFile to destinationFolder with replacing
	delete ThisFile of this_folder

The proper syntax your looking for is


tell application "Finder" to move file ThisFile to "Disk:Users:Me:Desktop:MovetestRec:" with replacing

Where “Disk” is the name of your hard disk and the rest is the paths to your destination folder. This is called “hardwiring” a path. To find the path of any folder, run


set theFolder to choose folder

Copy the path from the result window and paste it into the move statement. You should be getting close!
SC

When I run the move command to a destination on a local drive it will remove the file from the folder.
But I need to move the file to a SMB mounted sharepoint. It moves and replaces the file on the remote server. But doesn’t remove the file from the local drive. So I now move it twice. Once to the SMB server, then again to a local “processed” folder.
This seems to work ok. It leaves me with a folder full of old files, but I can deal with that somehow.

property destinationFolder : “RIP1DROP:” as alias ---- SMB mounted sharepoint
property tempProcessFolder : “Panther HD:DoneCopy:” as alias — Folder on local drive

on adding folder items to this_folder after receiving these_files

--delays and checks for transfer completion

repeat with ThisFile in these_files
	
	tell application "Finder" to set theString to name of ThisFile as string
	if (count theString) is equal to 13 then
		set theString to ((characters 1 thru 9 of theString as string) & "0" & character 10 of theString)
	else
		set theString to (characters 1 thru 11 of theString) as string
	end if
	tell application "Finder" to set name of ThisFile to theString
end repeat

repeat with ThisFile in these_files
	tell application "Finder" to move ThisFile to destinationFolder with replacing
end repeat

repeat with ThisFile in these_files
	tell application "Finder" to move ThisFile to tempProcessFolder with replacing
end repeat

end adding folder items to

I think its

:lol:
SC

Thanks for all the help Sitcom.
I’ve got a slick easy to understand script thats working very well for me now.