Editor crashes on "if not exists [file specification]"

Good Morning,

I’m writing a script to move files to a server hot folder. I have a routine which increments the file name if the file already exists on the server. It tests for the existence of a file by file specification and increments the proposed file name until the file specification does not exist.

The problem is that once the routine gets to the point where the file specification does not exist, Script Editor crashes (doesn’t help alot for debugging :frowning: ).

Here are samples of the variable contents. A comment in the script shows where these variables were gathered. In the first sample “93118 QXP7 Sample_Flat2 [02].pdf” already exists on the server. In the second sample, 93118 QXP7 Sample_Flat2 [03].pdf does not exist on the server. Note the “#0” in place of the extension in FilePath (which is a file specification) for the final increment.

FilePath: HOTFOLDERROOT:0-IncomingJobs:93100:93118:93118 QXP7 Sample_Flat2 [02].pdf
destPath: HOTFOLDERROOT:0-IncomingJobs:93100:93118:
fileName: 93118 QXP7 Sample_Flat2.pdf
newfileName: 93118 QXP7 Sample_Flat2 [02].pdf
fileNameExt: pdf"

FilePath: HOTFOLDERROOT:0-IncomingJobs:93100:93118:93118 QXP7 Sample_Flat2 [03].#0
destPath: HOTFOLDERROOT:0-IncomingJobs:93100:93118:
fileName: 93118 QXP7 Sample_Flat2.pdf
newfileName: 93118 QXP7 Sample_Flat2 [03].pdf
fileNameExt: pdf"

Once the routine tries to test for the [03] version of the file (or the first increment witch doesn’t exist), Script Editor unexpectedly quits.

OK, here’s the code for the incrementing routine. Any ideas?


on reName(theFile)
	set fileName to name of theFile
	set fileNameExt to name extension of theFile
	set fileNameExtLength to (length of fileNameExt) + 2
	set SEQ to 0
	repeat until not (exists filePath)
		set SEQ to SEQ + 1
		if SEQ is greater than 99 then
			display dialog "More than 100 files with this name - pick something else please!" buttons {"Cancel"} default button "Cancel" with icon 2
		end if
		if SEQ is less than 10 then
			set seqString to "0" & SEQ
		else
			set seqString to SEQ
		end if
		set newfileName to ((characters 1 through -fileNameExtLength of fileName) & " [" & seqString & "]." & fileNameExt) as string
		set filePath to ((destPath as string) & (newfileName as string)) as file specification
		-- Variable samples gathered here.
	end repeat
	set fileName to newfileName
end reName

Hi Tim,

the command exists to check the existence of a file or folder
works only with the Finder or System Events.

The second problem is, you define the variable newfileName after the repeat loop condition
Try this,


global destPath
set destPath to path to desktop as Unicode text
set aFile to "93118 QXP7 Sample_Flat2 [02].txt"
reName(destPath & aFile)


on reName(theFile)
	tell application "System Events" to set {name:fileName, name extension:fileNameExt} to file theFile
	if fileNameExt is missing value then set fileNameExt to ""
	if fileNameExt is not "" then set fileName to text 1 thru ((count fileName) - (count fileNameExt) - 1) of fileName
	
	set SEQ to 0
	repeat
		set SEQ to SEQ + 1
		if SEQ is greater than 99 then
			display dialog "More than 100 files with this name - pick something else please!" buttons {"Cancel"} default button "Cancel" with icon 2
		end if
		set seqString to " [" & text -2 thru -1 of ("0" & SEQ) & "]."
		set newfileName to text 1 thru -6 of fileName & seqString & fileNameExt
		tell application "System Events"
			if not (exists file (destPath & newfileName)) then exit repeat
		end tell
	end repeat
	set fileName to newfileName
end reName

Thanks Stefan

Pieces of the block you rewrote threw up errors, probably conflicted with other pieces of the script.

However, I got my block to work by yanking and reworking the “as file specification” commands.

Thanks for your suggestion,

T.

I just predefined those variables, which threw errors in your original script,
because they have been defined somewhere else :wink: