delete folders with files

Hey i need something that can delete folder a folder with files that are in it. so rmdir won’t work i don’t think.

Are you scripting it or using terminal?

And specifically, do you want to delete just a folder and preserve it’s contents somewhere, or something else?

Delete a folder that contains files. It’s for an application :smiley: I’d prefer to not open Terminal; but can use shell scripts "

do shell script

Delete a folder that contains files. It’s for an application :smiley: I’d prefer to not open Terminal; but can use shell scripts "

do shell script

What’s the matter with plain vanilla AppleScript? This puts all the files and the folder in the trash – my preferred approach so I can recover from an “Oh No!” experience.

set tFolder to choose folder with prompt "Choose a folder to delete, including contents"
tell application "Finder"
	delete (files of tFolder)
	delete tFolder
end tell

If you prefer a shorter version:

tell application "Finder" to delete (choose folder with prompt "Choose a folder to delete, including contents")

Ok thanks :smiley: How do i do it like

tell application "Finder" to delete POSIX path of "/Applications/Utilities"

Ofcourse i wouldn’t delete Utilities folder, but that’s just example :smiley: how would i go by that? i currently get an error.

tell application "Finder" to delete ((POSIX file "/Applications/Utilities") as alias)

HA i kinda figured it was something like that… Thanks!

Any ideas why this doesn’t work?

property folderOne : "Junk Folder"

set tFolder to alias "Media 1TB:My Files:" & folderOne & ""
tell application "Finder"
	delete (files of tFolder)
	--	delete tFolder
end tell



But yet this does


set tFolder to alias "Media 1TB:My Files:Junk Folder"
tell application "Finder"
	delete (files of tFolder)
	--	delete tFolder
end tell

Also on another note I’m not sure if using alias is the best approach, since the path should be what sticks not the location if I move it (if I understand alias correctly?). I couldn’t get a posix path to work for me using a property or the direct path as in the second example.

I am trying to make a script that I can update every 4 months quickly by changing property values at the top of the script.

Hi. Attempting to concatenate something in alias form to a string results in list items”the alias and the text; you could just use a string path instead of an alias, then use the folder specifier in the delete line.

delete (files of folder tFolder)

This doesn’t seem to work either, what am I doing wrong? I have tried POSIX path, applescript paths and other paths formats I could find on line to no avail.


property folderOne : "Junk Folder"

set tFolder to POSIX file path of "/Applications/Delete/" & folderOne & ""
tell application "Finder"
	delete (files of tFolder)
	--	delete tFolder
end tell


Finder doesn’t know anything about POSIX paths. Use a colon-delimited string path, as in your initial example, just leave out the alias bit. & “” adds an empty text (nothing) to the end of your statement, and isn’t needed.

property folderOne : "Junk Folder"

tell application "Finder" 
delete (files of folder ("Media 1TB:My Files:" & folderOne))
end

Oh boy, thank you, that works like a charm!

For what it’s worth I was struggling to get two properties in a single path to work properly but after many failed attempts got it to work.

property driveName : "Macintosh HD"

property folderOne : "Utilities"
property folderTwo : "Trash Folder"

property itemOne : "Delete.txt"


tell application "Finder"
	open document file ("" & driveName & ":Applications:" & folderOne & ":" & folderTwo & ":" & itemOne & ":")
end tell


(*
	tell application "Finder"
		delete (files of folder ("" & driveName & ":Applications:" & folderOne & ":" & folderTwo & ":"))
		delete (folders of folder ("" & driveName & ":Applications:" & folderOne & ":" & folderTwo & ":"))
	end tell
*)


This way you can have multiple property’s defined for folders in the path that can be updated at the top of the script, as well as a property for a file of course that you can open.

In this case “Macintosh HD: Applications:Utilities:Trash Folder:Delete.txt”

I couldn’t figure out a way to combine delete “files and folders of folder” as a single statement hence the two listings.

files and folders = items

or


tell application "Finder"
	items of folder "whatever" whose class is folder or class is document file
end tell

Thanks Stefan