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.
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
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…
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
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