script to periodically empty a folder?

Discussion in 'Mac Basics and Help' started by elbirth, Jun 23, 2006.

  1. elbirth macrumors 65816

    Jan 19, 2006
    North Carolina, US
    I'm trying to help someone work around a problem they're having. They use Pandora at work on an iBook, and because Pandora caches the songs it plays, their hard drive gets filled up after a while and runs out of disk space (not a whole lot of space free to begin with).
    We've tried changing the cache size settings in multiple browsers, but they don't seem to stick.

    My thought was to write up a simple script that you could run (maybe run it through iCal at specific intervals?) that would empty out the cache folder at a specified interval. The person I'm helping has no scripting knowledge at all, and while I kind of do, I don't know how to do this (or if it's possible).

    Does anyone have any ideas?
  2. Eraserhead macrumors G4


    Nov 3, 2005
    it's a pretty simple script you need the path of the folder you are trying to empty. First to test it works try the following script which will move the files to a test folder (called testdump) on the desktop

              do shell script ("mkdir ~/Desktop/testDump")
    end try
    do shell script ("mv \"/unix/style/path/to/the/folder/you/are/deleting/*\" ~/Desktop/testDump")
    the final /* needs to stay.
    once you get this working then create the following applescript
    do shell script ("rm \"/unix/style/path/to/the/folder/you/are/deleting/*\")
    then you can save this script and schedule it from an iCal event as an alarm. Please make sure the script is moving the files to the testdump folder before deleting them directly to make sure you are deleting the correct files.

    To find the UNIX style path you need get info on the folder containing the pandora files.
  3. elbirth thread starter macrumors 65816

    Jan 19, 2006
    North Carolina, US
    that works perfectly so far, thanks! one other thing though, since I know it works right, how would I go about emptying the trash? would something like this work, or would it need to be set up differently? (I'd rather ask before deleting something important on accident)

    do shell script ("mv \"Users/username/Library/Caches/Firefox/*\" /Users/username/.Trash")
    do shell script ("cd /Users/username/.Trash")
    do shell script ("sudo rm -ri /Users/username/.Trash/filetodelete")
  4. PatrickF macrumors 6502

    Feb 16, 2006
    Instead of messing around with iCal alarms I would just create a shell script. Save the following code somewhere (your home directory for example).

    rm -rf /path/to/files/*
    Then execute this to make it executable:

    sudo chmod +x ~/myscript
    Then use cron to execute this script whenever you want. Just type "crontab -e" in a Terminal and paste the following line into the editor - then just save and voila.

    # m h  dom mon dow   command                                                                                            
    */15 * * * * ~/myscript
    This tells cron to execute your script every day of the week every 15 minutes. Change the 15 to 30 for every half hour, etc. If you want to execute it every hour then change the "*/15 *" to "* */1" instead.

Share This Page