Deleting all folders fitting a naming convention.

It’s been quite awhile since I needed the help of you folks. Nice to know you’re all still here!

We are transferring a HUGE load work from another printing company and they use a different workflow than we do. As a result, we need to delete all folders that have a name ending in “.job” (and their contents). These folders come nested 2 or 3 levels deep. The folder that contains these offending folders is on a network drive.

I just need a droplet to drop the network folder on and let it rip.

Any quickie suggestions? I have access to both Panther 10.3.9 and Tiger 10.4.2 here at work.

on open theObject
	set deletePrompt to display alert "Are you sure you want to delete .job folders?" as critical message "This action cannot be undone." buttons {"Cancel", "Delete .job Folders"} default button "Delete .job Folders"
	if button returned of deletePrompt = "Delete .job Folders" then
		set theObjectPath to POSIX path of theObject
		do shell script "/usr/bin/find" & space & quoted form of theObjectPath & space & "-name \"*.job\" -type d -print0 | /usr/bin/xargs -0 /bin/rm -rf"
	else if button returned of deletePrompt = "Cancel" then
		return 0
	end if
end open

That’ll look inside the folder you drop on it and recursively find all folders inside that end with .job and delete them. Don’t click “Delete .job Folders” unless you reeeeeeally mean to. If you want to limit it to 3 folders deep, use this instead:

on open theObject
	set deletePrompt to display alert "Are you sure you want to delete .job folders?" as critical message "This action cannot be undone." buttons {"Cancel", "Delete .job Folders"} default button "Delete .job Folders"
	if button returned of deletePrompt = "Delete .job Folders" then
		set theObjectPath to POSIX path of theObject
		do shell script "/usr/bin/find" & space & quoted form of theObjectPath & space & "-maxdepth 3 -name \"*.job\" -type d -print0 | /usr/bin/xargs -0 /bin/rm -rf"
	else if button returned of deletePrompt = "Cancel" then
		return 0
	end if
end open

Thank you SO much. Fantastic. Could you possibly explain what you mean in the script by the “POSIX path” of theObject?

I wanted to pass the dropped folder’s path name to the shell. When you get an folder’s alias path in AppleScript, it looks like this:

The shell requires a POSIX path, which looks like this:

With or without a / on the end. AppleScript can convert alias paths to POSIX paths with the code I used. We’re not done yet, though, because you have to make sure you account for spaces between words in folder and file names:

If I send that to the shell, things will break, because a space is a command argument delimiter. Easily fixed, however, if you tell AppleScript to send it to the shell in a quoted fashion. Assuming thePath is the test folder path above:

quoted form of thePath

. . . looks like this when it hits the shell:

The quoting solves the whitespace issue.

Make sense?