Shelll Script mv and directory overwrite problem

Hi there

I have this short script which moves a directory into the the trash:

do shell script "mv " & foldercheck & " " & trashFolder

However, if I run it when a directory with the same name already exists in the trash, I get this error:

I had thought that the option “-f” would have solved this problem but no go.

Any suggestions?



wouldn’t it be much easier to use the Finder for this job??


Undoubtedly. Am trying to avoid the Finder though. The work around I have is to append the date/time to the trashed folder’s name. This avoids the issue.


Is there a way of using the Finder to trash stuff, but without having the trash sound?

You can disable all Finder sounds in the sound PrefPane or if you want to keep all sounds
except the trash sound, replace the specified sound file with a “silent” one

That’s an idea. But I really only want to temporarily disable the sound for the purposes of a script.

I have played around with the following script:

set gg to alert volume of (get volume settings)
set alert volume of (get volume settings) to 0
delay 1
set alert volume of (get volume settings) to gg
delay 1

But it does not do what I was hoping. Is there any way to set the volume alert value of (get volume settings)? This would be the best way so that music or anything else going on when the script runs, will not be affected.


Worked it out…

set gg to alert volume of (get volume settings)
set volume alert volume 0
delay 1
set volume alert volume gg
delay 1

Hi kiwilegal,

I guess you don’t want to use the «rm» command to permanently delete the folder, as you want to enable the user to retrieve the deleted folders from the trash.

If you don’t want to use the Finder, then you can also use code as follows to silently move a directory to the trash. The handler makes use of the «ditto» command to copy the directory to the trash folder. If the folder name of the directory already exists in the trash folder, the script adds a counter to the name (‘foldername’ => ‘foldername 1’).

set srcfolder to "DandyDisk:Users:martin:Desktop:test:"
set success to my trashfolder(srcfolder)

on trashfolder(srcfolderpath)
		set trashfolderpath to ((path to trash) as Unicode text)
		set srcfolderinfo to info for (srcfolderpath as alias)
		set srcfoldername to name of srcfolderinfo
		set srcfolderpath to quoted form of POSIX path of srcfolderpath
		set counter to 0
			if counter is equal to 0 then
				set destfolderpath to trashfolderpath & srcfoldername & ":"
				set destfolderpath to trashfolderpath & srcfoldername & " " & counter & ":"
			end if
				set destfolderalias to destfolderpath as alias
			on error
				exit repeat
			end try
			set counter to counter + 1
		end repeat
		set destfolderpath to quoted form of POSIX path of destfolderpath
		set command to "ditto " & srcfolderpath & space & destfolderpath
		do shell script command
		-- this won't be executed if the ditto command errors
		set command to "rm -r " & srcfolderpath
		do shell script command
		return true
	on error
		return false
	end try
end trashfolder

Cheers, Michael. Will try this out.