I have a very simple script I pinched from this forum, it works a treat but I have a lot of folders I need to clear out. But if there is an empty folder matching a specific name, I’d like to leave it alone. How do I add that variable to this script?
set pathoffolder to choose folder
--change folder here
tell application "Finder"
repeat with oneFolder in (get folders of pathoffolder)
if (count items) of oneFolder is 0 then delete oneFolder
end repeat
end tell
Just make a list of the names to ignore in “folderNamesToIgnore” and then run it like this…
set folderNamesToIgnore to {"some name", "another name"}
set pathoffolder to choose folder
--change folder here
tell application "Finder"
repeat with oneFolder in (get folders of pathoffolder)
set thisFolderName to name of oneFolder
if thisFolderName is not in folderNamesToIgnore then
if (count items) of oneFolder is 0 then delete oneFolder
end if
end repeat
end tell
How hard is it to make a droplet? I’ve been looking around and tweaked the script, but it just won’t work.
on open pathoffolder
set folderNamesToIgnore to {"Miscellaneous"}
--change folder here
tell application "Finder"
repeat with oneFolder in (get folders of pathoffolder)
set thisFolderName to name of oneFolder
if thisFolderName is not in folderNamesToIgnore then
if (count items) of oneFolder is 0 then delete oneFolder
end if
end repeat
end tell
end open
pathoffolder is a list. You access the items in a list by their index number. So in your case you want the first item or item at index 1. Try this. Notice I also added the “on run” handler. That happens when you double-click the droplet. By making it this way you can run the code by dropping things on it or double-clicking it.
I didn’t test this code but it should work…
-- this happens when you double-click the droplet
on run
set aFolder to choose folder
processOneFolder(aFolder)
end run
-- this happens when you drop folders on the droplett
-- note that we only work with the first dropped item
on open pathoffolders
processOneFolder(item 1 of pathoffolders)
end open
on processOneFolder(theFolder)
set folderNamesToIgnore to {"Miscellaneous"}
tell application "Finder"
repeat with oneFolder in (get folders of theFolder)
set thisFolderName to name of oneFolder
if thisFolderName is not in folderNamesToIgnore then
if (count items) of oneFolder is 0 then delete oneFolder
end if
end repeat
end tell
end processOneFolder
Glad to help. Here’s how you return the favor… keep learning applescript and help others to learn it. I was new to it too and the good people here helped me learn it. Now I help others.
I’ve made a version of it that drills down into the subfolders (which may be empty) and then deletes them. My scripting may be a bit dodgy, so if anyone has any advice on how I’ve done it I would be grateful.
--script to find empty folders and delete them
set myFolder to POSIX file "/Users/dwhitehead/Desktop/test" as alias
--change folder here
deleteEmptyFolders(myFolder)
to deleteEmptyFolders(folderPath)
tell application "Finder"
repeat with oneFolder in (get folders of folderPath)
if (count items) of oneFolder is 0 then
delete oneFolder
else
repeat with aFolder in (get folders of oneFolder)
my deleteEmptyFolders(oneFolder)
end repeat
end if
end repeat
end tell
end deleteEmptyFolders
Model: 2 x 2.8 GHz Quad-Core Intel Xeon
AppleScript: 2.1.2
Browser: Firefox 3.6.14
Operating System: Mac OS X (10.6)
I’m having problems combining the info in this thread to a script that suit my need.
A. I will use a folder script so I don’t need the droplet code.
B. I need the ignore list but empty folders inside an ignored folder should still be removed.
I have also noticed that the scripts here only removes the empty folder located deepest in the hierarchy. So if the structure is like this:
folder level one
2. folder level two
3. folder level three
then no. 3 will be deleted. But after this run folder no. 2 becomes empty too. But this folder will not be processed until the next time the script is run. I can live with this minor dilemma. Just thought I should mention it.
Sorry, I just realised that I had made changes to the script since posting. Here is the whole script, which also gives feedback on how many folders have been deleted.
-- script to find empty folders and delete them
global deleteCount
global timeStart
global timeFinished
global timeTaken
global totalTimeTaken
global parentPath
global folderName
global deleteLog
on run
tell application "Finder"
set theseFolders to selection
set folderCount to count of theseFolders
end tell
if folderCount is greater than 0 then
mainScript(theseFolders)
else
set theseFolders to choose folder with multiple selections allowed
mainScript(theseFolders)
end if
end run
-- main handler follows -----------------------------------------
---------------------------------------------------------------------
on mainScript(theseFolders)
set deleteLog to "" -- otherwise the log carries over from when last run
set timeTaken to 0 -- otherwise the time carries over from when last run
set totalTimeTaken to 0
repeat with thisFolder in theseFolders
set parentPath to GetParentPath(thisFolder)
getFolderName(thisFolder)
set timeStart to time string of (current date) -- capture the start time
deleteEmptyFolders(thisFolder) -- run the handler
set timeFinished to time string of (current date) -- capture the finish time
set timeTaken to (date timeFinished) - (date timeStart) -- take away start from finish to give time taken
set totalTimeTaken to totalTimeTaken + timeTaken
giveFeedback()
end repeat
writeLog()
end mainScript
-- sub handlers are below --------------------------------------
---------------------------------------------------------------------
on GetParentPath(theFile)
tell application "Finder" to return container of theFile as text
end GetParentPath
on getFolderName(aFolder)
set oldTIDs to AppleScript's text item delimiters
set folderName to aFolder as string
set AppleScript's text item delimiters to ":"
set folderName to text item -2 of folderName
set AppleScript's text item delimiters to oldTIDs
return folderName
end getFolderName
to deleteEmptyFolders(folderPath)
set deleteCount to 0 -- each time the handler is run the count is set to 0 so that the delete count does not tally up from before
tell application "Finder"
set allFolders to get (entire contents of folderPath)'s folders
set allFolders to reverse of allFolders
repeat with aFolder in allFolders
if (count items) of aFolder is 0 then
delete aFolder
set deleteCount to (deleteCount + 1)
end if
end repeat
end tell
end deleteEmptyFolders
on giveFeedback()
if deleteCount is greater than 0 then
set deleteCount to deleteCount as text
set deleteLog to deleteLog & return & folderName & return & deleteCount & " empty folders have been deleted." & return
display dialog folderName & return & deleteCount & " empty folders have been deleted." & return & "The script has taken " & totalTimeTaken & " seconds to run in total." giving up after 5
else
set deleteLog to deleteLog & return & folderName & return & deleteCount & " empty folders have been deleted." & return
display dialog "There were no empty folders in " & folderName giving up after 5
end if
end giveFeedback
on writeLog()
set theLogFile to (path to desktop as string) & "Empty_Folders_Delete_Log.txt"
try
set dataStream to open for access file theLogFile with write permission
set eof of dataStream to 0
write deleteLog to dataStream starting at eof -- as list
close access dataStream
on error
try
close access file theLogFile
end try
end try
end writeLog
I*m trying to build a folder script that will make a folder self maintained. It should show files not older than 1 year and no empty folders. There are 4 folders in the root level that never should be deleted even if they are empty.
I have the script that deletes old files in working order. But I need to add the part to clean up empty folders with a ignore function as shown above.