1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

DelTree

Discussion in 'T&T - Scripting' started by krischik, Sep 28, 2010.

  1. krischik

    krischik

    Messages:
    39
    Warning: This functions described here will delete a directory tree without questions ask and faster then you can press Ctrl-C to abort. So use with care.

    For a while now I have used the following function to delete directory trees:

    Code:
    function deltree () {
        for I in "${=@}";  do
            mv --verbose "${I}" "${I}.delete"
            rm --recursive --force "${I}.delete" &
        done;  unset I
    
        return
    }
    
    Today I finally found the time to create a similar function for TakeCommand which I like to share with you all:

    Code:
    @ECHO OFF
    
    SETLOCAL 
        DO I IN "%$"
            RENAME %I %I.DELETE
            DETACH ERASE /E /S /X /Y /Z %I.DELETE 
        ENDDO
    ENDLOCAL
    
    Originaly I wanted to create an alias as this is closer to the original idea of a shell function. But I could not get the FOR loop to work.

    Note that I did not use /K or /R for the ERASE command. You might want to consider the advantages and disadvantages of those options.
  2. samintz

    samintz Scott Mintz

    Messages:
    850
    V12 introduced the concept of single line DO.

    The following should all be on one line (email may break it across lines):

    alias deltrees=`do i in %$ (ren %i %i.delete & detach del /sexyz
    %i.delete)`

    -Scott

    krischik <> wrote on 09/28/2010 02:42:15 AM:


    trees:

    work.

  3. thedave

    thedave

    Messages:
    179
    On Tue, 28 Sep 2010 02:42:15 -0400, krischik
    <> was claimed to have wrote:


    This might be a stupid question, but is this faster/better than "rd /s
    /q"?
  4. krischik

    krischik

    Messages:
    39
    DETACH

    There are three tricks here:

    1. With the DETACH the actual delete moved into the background so the terminal is free to receive new command.
    2. The directory is renamed before the DETACH so you can create a new directory with the same name right away as well.
    3. A FOR loop allows you to delete more the one directory with a single command.

    All in all you have the feeling of instant deletion. Less then a sec no matter how big the directory is.

    Martin
  5. krischik

    krischik

    Messages:
    39
    DO vs FOR

    Now you got me confused. I always thought the FOR loop is the single line DO loop. Or rather the other way round: The DO loop is a multi line FOR loop.

    Martin
  6. samintz

    samintz Scott Mintz

    Messages:
    850
    DO vs FOR

    DO and FOR are separate commands. Each has its own syntax. FOR has been
    implemented slightly differently in each version of CMD.EXE that MS has
    distributed. Almost all the switches have been used to customize its
    behavior in one fashion or another. Most people complain when Rex
    modifies something (for the better) that breaks CMD compatibility.
    Usually, 4DOS/4NT/TCC was first and then MS comes along and introduces
    something new and breaks compatibilty.

    DO was introduced a while back as a multiline looping construct that was
    far more flexible and easier to debug than FOR. However, it was only
    usable in batch scripts. You couldn't use it in an alias or at the
    command line.

    V12 now allows DO to be used in aliases and at the command prompt.

    -Scott


    krischik <> wrote on 09/29/2010 05:41:41 AM:


  7. krischik

    krischik

    Messages:
    39
    Re: DO vs FOR

    Thank you for the extensive clarification.
  8. Rod Savard

    Rod Savard

    Messages:
    287
    Interesting approach renaming the folder and using detach. The rename would seem to also provide a sanity check to NOT delete anything if any file/folder is open in the selected folder...extra safety precaution.
  9. krischik

    krischik

    Messages:
    39
    Did not think of that - because the original is from unix where a rename will work when a file is open. Thats because on unix only the inode is locked but not the filename.

    Martin

Share This Page