delete files of folders

Dear All,

I have some folders says Folder1, Folder2, Folder3, Folder4, etc. in a folder called “RootFolder”. I want to delete the entire contents of all folders except only Folder2. I don’t want to delete any contents of Folder2 only. Is it possible through applescript.

Thanks & Regards,

If I’m following you correctly then this should do the the trick

set excludeList to {"folder2"}
set rootFolder to (choose folder) as alias

tell application "Finder"
	set theFolders to folders of rootFolder
	repeat with aFolder in theFolders
		if name of aFolder is not in excludeList then delete entire contents of aFolder
	end repeat
end tell

And here’s a shell incarnation as well

set exclude to "folder2"
set rootFolder to quoted form of POSIX path of (choose folder)

do shell script "/usr/bin/find " & rootFolder & " -mindepth 1 -maxdepth 1 -type d ! -name '" & exclude & "' | /usr/bin/xargs -I {} /usr/bin/find {} -mindepth 1 | /usr/bin/xargs /bin/rm -rf"

And another shell version because we don’t really need to perform two find commands for what you want to do

set exclude to "folder2"
set rootFolder to quoted form of POSIX path of (choose folder)

do shell script "/usr/bin/find " & rootFolder & " -mindepth 1 -maxdepth 1 -type d ! -name '" & exclude & "' | /usr/bin/xargs -I {} /bin/echo /bin/rm -rf {}/* | /bin/sh"

–i’ve a simple alternative…

tell application "Finder"
	set target_f to (choose folder) as alias
end tell

--You can set target_f instead of front window, if your "rootfolder" is not open and not the first activ window in finder.

tell application "Finder" to delete (every folder of target_f where name is not "folder2")

--or, if your "folder2" has a label.. (label 3=yellow)

tell application "Finder" to delete (items of front window whose label index ≠3)

Problem with that is it deletes the depth 1 folders which the poster indicated he did not want deleted. This is in the same vein though and will accomplish the task of not deleting the depth 1 folders

set exclude to "folder2"
set target_f to (choose folder) as alias
tell application "Finder" to delete every item of (entire contents of (folders of target_f whose name is not exclude))

This script could work for me with one minor tweak. I need to delete any/all files AND folders that reside inside 2 separate subfolders in “UserDefinedFolders” which is already defined in excludeList below.
Those subfolders are…

UserDefinedFolders/Input Files/
UserDefinedFolders/Exports/

Any other subfolders inside “UserDefinedFolders” should remain untouched.



set excludeList to {"Control", "DigitalPrint", "Fonts", "HotFolders", "System", "TransientLayouts", "UserDefinedFolders", "WebDownloads", "WebUploads"}
set rootFolder to (choose folder) as alias

tell application "Finder"
	set theFolders to folders of rootFolder
	repeat with aFolder in theFolders
		if name of aFolder is not in excludeList then delete aFolder
	end repeat
end tell


You could use that code a 2nd time, with a small change:

set excludeList to {"Control", "DigitalPrint", "Fonts", "HotFolders", "System", "TransientLayouts", "UserDefinedFolders", "WebDownloads", "WebUploads"}
set rootFolder to (choose folder) as alias

tell application "Finder"
	set theFolders to folders of rootFolder
	repeat with aFolder in theFolders
		if name of aFolder is not in excludeList then delete aFolder
	end repeat
	
	-- now go deeper
	set includeList to {"Input files", "Exports"}
	set rootFolder to folder "UserDefinedFolders" of rootFolder
	
	set theFolders to folders of rootFolder
	repeat with aFolder in theFolders
		if name of aFolder is in includeList then delete aFolder
	end repeat
	
end tell

Thanks, but that seems to delete the actual subfolders (below) instead of just all the contents inside them.

UserDefinedFolders/Input Files/
UserDefinedFolders/Exports/


set excludeList to {"Control", "DigitalPrint", "Fonts", "HotFolders", "System", "TransientLayouts", "UserDefinedFolders", "WebDownloads", "WebUploads"}
set rootFolder to (choose folder) as alias

tell application "Finder"
   set theFolders to folders of rootFolder
   repeat with aFolder in theFolders
       if name of aFolder is not in excludeList then delete aFolder
   end repeat
   
   -- now go deeper
   set includeList to {"Input files", "Exports"}
   set rootFolder to folder "UserDefinedFolders" of rootFolder
   
   set theFolders to folders of rootFolder
   repeat with aFolder in theFolders
       if name of aFolder is in includeList then delete aFolder
   end repeat
   
end tell

Ah, yes. Missed that. You need to add entire contents of in the 2nd part, as in James’ script in post #2:

if name of aFolder is in includeList then delete entire contents of aFolder

That wasn’t too hard, was it? :slight_smile: