Delete empty folders

Discussion in 'Mac Programming' started by sealdriver, Feb 19, 2012.

  1. macrumors newbie

    Joined:
    Feb 19, 2012
    #1
    Hi
    I want a script that will delete any empty folders of a given folder. I realise that trying to make it checked nested folders may be very difficult so I started by just deleting the empty folders at the top level. However, my script doesn't work. as a complete beginner I hope someone can point me right.
    Ideally I would like the script to ask me which folder to check when it first runs but I was getting totally lost with that. Here's what I have


    tell application "Finder"
    set folderPath to "/Users/Pat/Desktop/Folder1"

    repeat with checkFolder in (get folders of folderPath)
    if (count items) of checkFolder is 0 then delete checkFolder
    end repeat
    end tell


    Thanks
     
  2. macrumors 6502a

    Joined:
    Nov 9, 2010
    #2
    Terminal

    find . -type d -empty -print0 | xargs -0 rmdir

    Much easier.
     
  3. thread starter macrumors newbie

    Joined:
    Feb 19, 2012
    #3
    Thanks for that but would that not simply delete all empty folders on the disk?
    That's not what I wanted to achieve.

    Cheers
     
  4. macrumors 6502a

    Joined:
    Nov 9, 2010
    #4
    It starts from where you currently are. Note the ".".
    If you change the directory to "startHere" than it will start there ;)

    If that's not what you meant, I sincerely don't understand your question.

    You want to recursivly delete all empty folders under a certain folder, right?
     
  5. thread starter macrumors newbie

    Joined:
    Feb 19, 2012
    #5
    Hi

    Thanks, I hadn't realised it started from your current location, I assumed it would just sweep the disk for empty folders.

    Cheers
     
  6. macrumors 6502a

    Joined:
    Nov 9, 2010
    #6
    Oh, one last comment.

    If a folder gets empty because its last entry was an empty folder, it might not get deleted. Do do that, find needs to descend into folders first.

    This would be done adding the "-d" option ...

    Cheers
     

Share This Page