Help with adding extension with duplicate command

Let me start out by saying I’m sorry for asking this question. I’ve done a search for this same question and found a bunch of hits, but for some reason, my brain just won’t adapt the solutions for my situation.

I’m trying to copy a file from a local folder to a folder on our server, if the file already exists in that location, I’m want to add an extension to the file being copied. So that it would be “FileName.eps”, then “FileName - 1.eps”, then “FileName - 2.eps”. I don’t really care how the extension is implemented, I just need to be able to distinguish between them.

I’m also getting errors when I try to verify that the size of the source file is equal to the size of the destination file. But I think that’s probably because I’m not setting the destination file’s name correctly.

Here’s what I have:


set dFolder to "X Server:FTP Backup:"
set sourceFolder to choose folder with prompt "Select a folder to copy from"
	--Get a list of files/folders
	set fileList to list folder sourceFolder without invisibles
	
	--Start Loop
	repeat with thisFile in fileList
		--Set path to thisFile
		set filePath to ((sourceFolder as text) & thisFile) as alias
		set fileInfo to (info for filePath)
		set fileName to (name of fileInfo)
		set fileExt to (name extension of fileInfo)
		set extLength to (length of fileExt)
		set baseName to text 1 thru -(extLength + 2) of fileName
		
		set duplicateExt to ""
		set dup to 0
		--Since file doesn't contain the flag, the file must be copied and a tag added to it.
		--Check to see if file already exists, if it does then add an extention to it
		tell application "Finder"
			repeat
				if (item (dFolder & ":" & thisFile & duplicateExt)) exists then
					set dup to dup + 1
					set duplicateExt to " - " & dup
				else
					exit repeat
				end if
			end repeat
			
			set thisName to baseName & " - " & dup & "." & fileExt
			display dialog thisName as text
			--Copy file to destination folder
			set thisDuplicate to duplicate filePath
			set name of thisDuplicate to thisName
			move ((sourceFolder as text) & thisName) to folder (dFolder as alias)
			delete ((sourceFolder as text) & thisName)
			--set name of thisDuplicate to thisName
		end tell
		
		set s_info to info for filePath
		set d_path to (dFolder & ":" & thisFile)
		set d_info to info for d_path
		
		if size of s_info = size of d_info then set errorMessage to "File size does not match"
		
		if errorMessage = "" then
			set comment of thisFile to flag & backupDate
		else
			set comment of thisFile to "Error: " & errorMessage
		end if
	end repeat


Again, I’m sorry. I’m sure the answer is staring at me, but I just can’t see it.

Thanks
Brian

Hi,

I see one colon too much

set dFolder to "X Server:FTP Backup:"
.
if (item (dFolder & ":" & thisFile & duplicateExt)) exists then
.
set d_path to (dFolder & ":" & thisFile)

try

set dFolder to "X Server:FTP Backup:"
.
if (item (dFolder &  thisFile & duplicateExt)) exists then
.
set d_path to (dFolder & thisFile)

The size calculation can take some time especially on the server.
You get missing value while calculating.
Using a repeat loop until the value is not missing value can help

Thanks Stefan,
I actually added that line to try to make the script look complete. There’s a lot of the script that I didn’t post. But you’re right, I did have an extra colon.

The part of this that I really need help with, is renaming the destination file. When I run the entire script, I’m getting unpredictable results. One time I ran it I got “Filename.eps”, then “Filename - 1.eps” then “Filename - 1 - 0.eps”, etc.

I think that when I ran it the first time, it created “Filename - 1.eps”, then when I ran it a second time, it created “Filename - 1 - 0.eps” because the file “Filename - 1.eps” didn’t get deleted.

Thanks again!
Brian

for a similar case I wrote a subroutine to check the filename

set newName to checkFileName(fDir, name of (info for something), " - ")

on checkFileName(fDir, fName, separator) -- fDir can be alias or path string
	set fDir to fDir as Unicode text
	try
		set f to (fDir & fName) as alias
		set {name:Nm, name extension:Ex} to info for f
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set idx to 0
		repeat
			set idx to idx + 1
			set checkName to (fDir & Nm & separator & (idx as string) & "." & Ex)
			try
				checkName as alias
			on error
				return checkName
			end try
		end repeat
	on error
		return (fDir & fName)
	end try
end checkFileName

I’d like to clarify what you want to do. You’re duplicating files from a local machine to a server. If a file on the server has the same name as one to be duplicated, you want to add a postfix to your file’s base name, keeping the old extension. The postfix is not there on originals, but should be incremented if it exists. If that’s it, then two questions: 1) do you want a character between the postfix number and the base name (like myfile-02.eps. I wouldn’t put spaces in the name)? 2) what is a character that is a) legal in a file name, and b) never used in original base names?

Thanks guys for the response.
Adam:
You’ve got what I’m looking for correct. I’m using an X Serve running os 10.4.8. I’ve never had any problems with a hyphen being used in a file name. I don’t typically use hyphens in my file naming either. However, I do sometimes use underscores. You’re right about not putting spaces in the name. I don’t want to put spaces in the name. So the example you gave would be perfect. “Filename.eps”, “Filename-1.eps”, “Filename-2.eps”, would be perfect. The leading zeros are nice, but not necessary. I don’t expect this to happen very often, but I need to error check for it, just in case. Thanks again!

edit for clarification:

  1. do you want a character between the postfix number and the base name (like myfile-02.eps. I wouldn’t put spaces in the name)? Yes
  2. what is a character that is a) legal in a file name, and b) never used in original base names? a Hyphen

Stefan:
Thank you for taking the time to send me your code. I follow everything except for one thing:

   if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm

What does that do? I get the if statement, but It seems to me that if a file was named “Filename.eps”, that Nm would be set to text 1 thru ((8) - (3) - 1) of Nm, which is text 1 thru 4 of Nm, which would result in “File” being set to Nm. Clearly I’ve got something wrong, but can you explain how this works?

Thanks, this help is much appreciated!
Brian

It strips the name from the extension, e.g. you have the filename “Filename.eps”, you get Nm=Filename ans Ex=eps

its not text 1 thru ((8) - (3) - 1) of Nm, it is text 1 thru ((12) - (3) - 1) of Nm because the property name includes the extension

Stefan,
Thanks for the quick reply. One last question,

Using the example file “Filename.eps”, does this:
set {name:Nm, name extension:Ex} to info for f

set Nm to “Filename.eps”? I assumed it would set it for “Filename”, and Ex would then be “eps”, correct?

Thanks again,
Brian

No, Nm is “Filename.eps” and Ex is “eps”, that why we need the lines to strip the name properly

An alternative way to extract a basename (sans extension) is like this – I’ve stuck in the choose file for illustration

set f to (choose file) -- for illustration
tell (info for f) to set {Nm, Ex} to {name, name extension}
set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
BN

where offset finds the location of the first character (as a count from the beginning of the string) of the first instance of “.” & the extension, so (get offset of “.” & Ex in Nm) -1 is the position of the last character of the base name.

And for those who like one-liners (which I confess I do), though this one is slower than the one above because it calls for the name twice

tell (info for (choose file)) to set basename to name's text 1 thru ((my (offset of ("." & name extension) in name)) - 1)

Sorry Adam, I don’t like these because they fail, if there is more than one dot in the filename

I worked it into my script and it seems to work perfectly.
Thanks again!
Brian