DelTree

#1
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 "${[email protected]}";  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.
 

samintz

Scott Mintz
May 20, 2008
1,272
11
Solon, OH, USA
#2
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:


> *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 "${[email protected]}"; 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.
>
>
>
>
 
#3
On Tue, 28 Sep 2010 02:42:15 -0400, krischik
<> was claimed to have wrote:


>*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.
This might be a stupid question, but is this faster/better than "rd /s
/q"?
 
#4
DETACH

This might be a stupid question, but is this faster/better than "rd /s /q"?
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
 

samintz

Scott Mintz
May 20, 2008
1,272
11
Solon, OH, USA
#6
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:


> ---Quote (Originally by samintz)---
> V12 introduced the concept of single line DO.
> ---End Quote---
> Now you got me confused. I always thought the FOR loop *is* the
> single line DO loop.
>
> Martin
>
>
>
>
 
May 26, 2008
492
3
#8
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.