8 Replies Latest reply: Feb 13, 2008 1:56 PM by Camelot
BenChase Level 1 Level 1 (10 points)
I want to delete files of a certain folder without moving them to the trash.

I have this script but I don't know how to get it to select the contents of the folder:

set theFolder to choose folder
set theFolder to quoted form of (POSIX path of theFolder as string)

do shell script "rm -r " & theFolder


Am I even on the right track here? Maybe there is a safer way then using "rm -r"?
  • Camelot Level 8 Level 8 (46,450 points)
    I have this script but I don't know how to get it to select the contents of the folder:


    As written, your script will delete the folder and everything within it. That's because you're passing the -r (recursive) switch to rm.

    If you just want to delete the contents of the folder and not the folder itself, change the shell command to:

    <pre class=command>do shell script (rm -r " & theFolder & "*")</pre>

    The addition of the "*" indicates the contents of the folder, rather than the folder itself.
  • BenChase Level 1 Level 1 (10 points)
    You are a genius. That worked great.

    What I would like to do now is have it run at logout. I want to use this to clean up the downloads folder when the users are finished and logout.

    Should I attach this to a folder as a folder action or something like that, or put it as a logout script in WGM?

    Any advice?

    Thanks!
  • Camelot Level 8 Level 8 (46,450 points)
    Should I attach this to a folder as a folder action or something like that


    Folder Actions won't help here. There is no Folder Action that is fired when you logout. Therefore some kind of logout script is the way to go.
  • BenChase Level 1 Level 1 (10 points)
    Thanks again Camelot!

    If I would like to set up this script so that I don't have to choose the path I know that I can do this:

    set theFolder to "Macintosh HD:Users:ben:Desktop:Move to Trash:"

    but what if I wanted the user to be who ever is logged in and not "ben"?
    Do I have to make a separate script for everyone one of our users OR can I put a variable in there that would set the path for whatever user is logged in?

    The folder would be in the same place on every user the only difference would be the short name.
  • Camelot Level 8 Level 8 (46,450 points)
    but what if I wanted the user to be who ever is logged in and not "ben"?


    That's what the 'path to' command is for. It will return the current location of common directories (desktop, preferences, library, etc., etc.) based on the current user and language settings (the desktop is not called 'Desktop' when running a French OS, for example).

    <pre class=command>set theFolder to (path to Desktop as text) & "Move to Trash:"</pre>
  • BenChase Level 1 Level 1 (10 points)
    Camelot,

    I think this is my last question, again, thanks for the help!

    This is what I have right now but its going to the HD library instead of the user library:

    set theFolder to (path to library folder as text) & "move to trash:"
    set theFolder to quoted form of (POSIX path of theFolder as string)

    do shell script ("rm -r " & theFolder & "*")


    I know its probably just one word but I am not sure how to find what word it should be.
  • BenChase Level 1 Level 1 (10 points)
    Never mind...

    I added this "from user domain" and its seems to work great. Thanks for all your help.

    Here is what the final script ended up looking like:


    set theFolder to (path to library folder from user domain as text) & "move to trash:"
    set theFolder to quoted form of (POSIX path of theFolder as string)

    do shell script ("rm -r " & theFolder & "*")
  • Camelot Level 8 Level 8 (46,450 points)
    Glad you found the answer.

    In your earlier example you were using a folder on the Desktop. Since the Desktop only exists at a user level, there's no need to specify 'from user domain'.

    'Library', on the other hand, exists at both the user and system levels, therefore you need to be more explicit about which one you want.