Rename files added to folder, move & keep existing extension

I’m stumpped!

I have been trying to create a script that will rename files with the current date/time as they are added to a folder but retain the current extension.

I had managed to get each file renamed with the date and moved to a “renamed” folder, but without the original extension. As soon as I try to amend the new name to add the original extension the file just sits in the folder with the action attached and does nothing.

I am getting the date with:

set newName to (do shell script "date +%y%m%d-%H%M%S")

I have now completely broken my original script that renamed then moved (without the original extension) by trying different versions. (I must remember to do a save as whenever I amend a scrip that works to some degree!!)

Any help, hints or advice greatly appreciated.

OSX: 10.4.11

Hi all,

I have found (on another machine) the script that works without retaining the extension (.pdf, .eps, .ai, .tiff etc…)

Is there a way to keep the old extension (probably only 2, 3 or 4 characters after the . !) and add this to the new name?

Again, any help, advice or suggestions greatly appreciated.

on adding folder items to this_folder after receiving added_items
	set prefix to (do shell script "date +%y%m%d%H%M%S")
	set target_folder to (path to desktop as string) & "Renamed"
	tell application "Finder"
		try
			get target_folder as alias
		on error
			make new folder at desktop with properties {name:"Renamed"}
		end try
		repeat with anItem in added_items
			set theName to name of anItem
			set theName to prefix
			set name of anItem to theName
			move anItem to folder "Renamed"
			delay 1
		end repeat
	end tell
end adding folder items to

Hi,


on adding folder items to this_folder after receiving added_item
	set target_folder to (path to desktop as text) & "Renamed:"
	try
		get target_folder as alias
	on error
		tell application "Finder" to make new folder at desktop with properties {name:"Renamed"}
	end try
	repeat with anItem in added_items
		set prefix to (do shell script "date +%y%m%d%H%M%S")
		set newFile to (POSIX path of target_folder & prefix & "." & name extension of (info for anItem))
		do shell script "/bin/mv " & quoted form of POSIX path of anItem & space & quoted form of newFile
		delay 1
	end repeat
end adding folder items to

Hi Sefan,

Thanks for the quick reply! I have tried your script and when a new file is dropped in, it creates the destination folder (if it is not present) but then nothing!! - The original file sits in the “drop” folder and does not rename or move!??

This is similar to everything else I have tried - in that if I try to keep the extension the original file sits in the “drop” folder!

Any thooughts?

it’s added_items in the first line

Stefan,

That has done the trick!

Many, many thanks!

(I’m sure that was a deliberate mistake to test me!!! Ha ha…!)

Thanks again.

No, it was a silly copy&paste mistake :wink: