View Full Version : DELTREE equivalent


Andrew Aronoff
I'm looking for a replacement for DELTREE.EXE under W2K. I've read in
various Usenet posts that RD and/or DEL can be used, but I haven't
found this to be true in my tests.

I'm looking for a command to replace the following:

DELTREE /Y C:\TEMP\

This single command deletes the entire contents of the directory
C:\TEMP (including subdirectories and their contents) _without_
deleting the directory itself.

RD /Q /S C:\TEMP deletes the TEMP directory, too.

Is there a closer fit? (If not, I'll stick with DELTREE, which works
perfectly well in a CMD.EXE shell under W2K and WXP.)

regards, Andy
--
**********

Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com

To identify everything that starts up with Windows, download
"Silent Runners.vbs" at www.silentrunners.org

**********

NayJo
rd /s

see rd /?

J


"Andrew Aronoff" wrote in message
news:blfnj1t3do6kuc46rnmcqdfvi1h15eedf5@4ax.com...
> I'm looking for a replacement for DELTREE.EXE under W2K. I've read in
> various Usenet posts that RD and/or DEL can be used, but I haven't
> found this to be true in my tests.
>
> I'm looking for a command to replace the following:
>
> DELTREE /Y C:\TEMP\
>
> This single command deletes the entire contents of the directory
> C:\TEMP (including subdirectories and their contents) _without_
> deleting the directory itself.
>
> RD /Q /S C:\TEMP deletes the TEMP directory, too.
>
> Is there a closer fit? (If not, I'll stick with DELTREE, which works
> perfectly well in a CMD.EXE shell under W2K and WXP.)
>
> regards, Andy
> --
> **********
>
> Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com
>
> To identify everything that starts up with Windows, download
> "Silent Runners.vbs" at www.silentrunners.org
>
> **********



NayJo
Apologies Andrew, I should have read further....

J


"NayJo" wrote in message
news:e2HBF9OxFHA.3856@tk2msftngp13.phx.gbl...
> rd /s
>
> see rd /?
>
> J
>
>
> "Andrew Aronoff" wrote in message
> news:blfnj1t3do6kuc46rnmcqdfvi1h15eedf5@4ax.com...
> > I'm looking for a replacement for DELTREE.EXE under W2K. I've read in
> > various Usenet posts that RD and/or DEL can be used, but I haven't
> > found this to be true in my tests.
> >
> > I'm looking for a command to replace the following:
> >
> > DELTREE /Y C:\TEMP\
> >
> > This single command deletes the entire contents of the directory
> > C:\TEMP (including subdirectories and their contents) _without_
> > deleting the directory itself.
> >
> > RD /Q /S C:\TEMP deletes the TEMP directory, too.
> >
> > Is there a closer fit? (If not, I'll stick with DELTREE, which works
> > perfectly well in a CMD.EXE shell under W2K and WXP.)
> >
> > regards, Andy
> > --
> > **********
> >
> > Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com
> >
> > To identify everything that starts up with Windows, download
> > "Silent Runners.vbs" at www.silentrunners.org
> >
> > **********
>
>



Mark V
In microsoft.public.win2000.cmdprompt.admin Andrew Aronoff wrote:

> I'm looking for a replacement for DELTREE.EXE under W2K. I've
> read in various Usenet posts that RD and/or DEL can be used, but
> I haven't found this to be true in my tests.
>
> I'm looking for a command to replace the following:
>
> DELTREE /Y C:\TEMP\
>
> This single command deletes the entire contents of the directory
> C:\TEMP (including subdirectories and their contents) _without_
> deleting the directory itself.
>
> RD /Q /S C:\TEMP deletes the TEMP directory, too.

Run that from *within* the c:\temp\ location.

> Is there a closer fit? (If not, I'll stick with DELTREE, which
> works perfectly well in a CMD.EXE shell under W2K and WXP.)
>
> regards, Andy


R Harrison
You could scrip it so that you do the RD function, which will delete the
entire structure as you mentioned, then do an MD Temp to recreate it again.

The only way to remove all directories in C:\Temp is to RD them 1 at a time,
because RD does remove the root directory in the command.

Suggested bacth file
*************************
c:
cd\
RD /S /Q C:\Temp
md temp
*************************



"Andrew Aronoff" wrote in message
news:blfnj1t3do6kuc46rnmcqdfvi1h15eedf5@4ax.com...
> I'm looking for a replacement for DELTREE.EXE under W2K. I've read in
> various Usenet posts that RD and/or DEL can be used, but I haven't
> found this to be true in my tests.
>
> I'm looking for a command to replace the following:
>
> DELTREE /Y C:\TEMP\
>
> This single command deletes the entire contents of the directory
> C:\TEMP (including subdirectories and their contents) _without_
> deleting the directory itself.
>
> RD /Q /S C:\TEMP deletes the TEMP directory, too.
>
> Is there a closer fit? (If not, I'll stick with DELTREE, which works
> perfectly well in a CMD.EXE shell under W2K and WXP.)
>
> regards, Andy
> --
> **********
>
> Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com
>
> To identify everything that starts up with Windows, download
> "Silent Runners.vbs" at www.silentrunners.org
>
> **********



Mark V
In microsoft.public.win2000.cmdprompt.admin R Harrison wrote:

> You could scrip it so that you do the RD function, which will
> delete the entire structure as you mentioned, then do an MD Temp
> to recreate it again.
>
> The only way to remove all directories in C:\Temp is to RD them
> 1 at a time, because RD does remove the root directory in the
> command.

Not if you are "in" it.

>
> Suggested bacth file
> *************************
> c:
> cd\
> RD /S /Q C:\Temp
> md temp
> *************************
>
>
>
> "Andrew Aronoff" wrote in
> message news:blfnj1t3do6kuc46rnmcqdfvi1h15eedf5@4ax.com...
>> I'm looking for a replacement for DELTREE.EXE under W2K. I've
>> read in various Usenet posts that RD and/or DEL can be used,
>> but I haven't found this to be true in my tests.
>>
>> I'm looking for a command to replace the following:
>>
>> DELTREE /Y C:\TEMP\
>>
>> This single command deletes the entire contents of the
>> directory C:\TEMP (including subdirectories and their contents)
>> _without_ deleting the directory itself.
>>
>> RD /Q /S C:\TEMP deletes the TEMP directory, too.
>>
>> Is there a closer fit? (If not, I'll stick with DELTREE, which
>> works perfectly well in a CMD.EXE shell under W2K and WXP.)
>>
>> regards, Andy
>> --
>> **********
>>
>> Please send e-mail to: usenet (dot) post (at) aaronoff
>> (dot) com
>>
>> To identify everything that starts up with Windows,
>> download
>> "Silent Runners.vbs" at www.silentrunners.org
>>
>> **********
>
>
>


billious

"Andrew Aronoff" wrote in message
news:blfnj1t3do6kuc46rnmcqdfvi1h15eedf5@4ax.com...
> I'm looking for a replacement for DELTREE.EXE under W2K. I've read in
> various Usenet posts that RD and/or DEL can be used, but I haven't
> found this to be true in my tests.
>
> I'm looking for a command to replace the following:
>
> DELTREE /Y C:\TEMP\
>
> This single command deletes the entire contents of the directory
> C:\TEMP (including subdirectories and their contents) _without_
> deleting the directory itself.
>
> RD /Q /S C:\TEMP deletes the TEMP directory, too.
>
> Is there a closer fit? (If not, I'll stick with DELTREE, which works
> perfectly well in a CMD.EXE shell under W2K and WXP.)
>
> regards, Andy
> --


How about (as a batch file, obviously)

xcopy /t /e c:\temp c:\somejunkname\
rd /q/s c:\temp
ren c:\somejunkname temp

You could possibly build somejunkname (just in case somejunkname might
already exist) using something like

set yjn=x
:loop
if exist c:\%yjn%* set yjn=%yjn%q&goto loop

which should set %yjn% (Your Junk Name) to "x" followed by as many "q"s as
are required to make the string unique. use %yjn% in place of somejunkname
in the above, clean up yjn using

set yjn=

to remove yjn from the environment, and job done.

HTH

....Bill




Andrew Aronoff
>> RD /Q /S C:\TEMP deletes the TEMP directory, too.
>
>Run that from *within* the c:\temp\ location.

Thanks. Didn't know that. That's certainly a very close fit.

regards, Andy
--
**********

Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com

To identify everything that starts up with Windows, download
"Silent Runners.vbs" at www.silentrunners.org

**********

Andrew Aronoff
>> RD /Q /S C:\TEMP deletes the TEMP directory, too.
>
>Run that from *within* the c:\temp\ location.

Oops! I tried it out and it does what's needed, but it throws the
error, "The process cannot access the file because it is being used by
another process." Do you know of any way around that?

regards, Andy
--
**********

Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com

To identify everything that starts up with Windows, download
"Silent Runners.vbs" at www.silentrunners.org

**********

Andrew Aronoff
>How about (as a batch file, obviously)
>
>xcopy /t /e c:\temp c:\somejunkname\
>rd /q/s c:\temp
>ren c:\somejunkname temp

Thanks for the suggestion. I think the /e parameter is best omitted
since it copies the subdirectories, which DELTREE would have deleted.

IAC, I think I'll stick with DELTREE, which is simpler to deploy and
run.

regards, Andy
--
**********

Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com

To identify everything that starts up with Windows, download
"Silent Runners.vbs" at www.silentrunners.org

**********

Bill Stewart
Andrew Aronoff wrote:

> Oops! I tried it out and it does what's needed, but it throws the
> error, "The process cannot access the file because it is being used by
> another process." Do you know of any way around that?

Yes. Redirect STDERR to NUL. Sample script fragment:

pushd %1 || goto :EOF
rd /q /s . 2> NUL
popd

--
Bill Stewart

Andrew Aronoff
>> Oops! I tried it out and it does what's needed, but it throws the
>> error, "The process cannot access the file because it is being used by
>> another process." Do you know of any way around that?
>
>Yes. Redirect STDERR to NUL. Sample script fragment:
>
>pushd %1 || goto :EOF
>rd /q /s . 2> NUL
>popd

Sorry, I wasn't clear. I wanted to know how to avoid the error, not
suppress the display.

regards, Andy
--
**********

Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com

To identify everything that starts up with Windows, download
"Silent Runners.vbs" at www.silentrunners.org

**********

David Candy
It is relying on the error to work.

--
--------------------------------------------------------------------------------------------------
http://webdiary.smh.com.au/archives/_comment/001075.html
=================================================
"Andrew Aronoff" wrote in message news:egsoj1haaqshg87uubsjabhn89d6195lsr@4ax.com...
>>> Oops! I tried it out and it does what's needed, but it throws the
>>> error, "The process cannot access the file because it is being used by
>>> another process." Do you know of any way around that?
>>
>>Yes. Redirect STDERR to NUL. Sample script fragment:
>>
>>pushd %1 || goto :EOF
>>rd /q /s . 2> NUL
>>popd
>
> Sorry, I wasn't clear. I wanted to know how to avoid the error, not
> suppress the display.
>
> regards, Andy
> --
> **********
>
> Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com
>
> To identify everything that starts up with Windows, download
> "Silent Runners.vbs" at www.silentrunners.org
>
> **********

Mark V
In microsoft.public.win2000.cmdprompt.admin Andrew Aronoff wrote:

>>> Oops! I tried it out and it does what's needed, but it throws
>>> the error, "The process cannot access the file because it is
>>> being used by another process." Do you know of any way around
>>> that?
>>
>>Yes. Redirect STDERR to NUL. Sample script fragment:
>>
>>pushd %1 || goto :EOF
>>rd /q /s . 2> NUL
>>popd
>
> Sorry, I wasn't clear. I wanted to know how to avoid the error,
> not suppress the display.

This "error" is part of the "but don't remove the target directory
itself" and for this purpose is entirely innocuous and expected.

I'd be careful with DELTREE since it is not supplied with W2K. It
may fail under conditions found but for which it is was not
designed.

RMDIR is "native". Enhance your batch usage by confirming the CD
after you have changed into the target and before issuing
RD /s /q .

And for anyone trying to justify a method of just removing the
target tree and then recreating the top-level directory, methinks
you are not taking security into account (NTFS ACLs).


Andrew Aronoff
Thanks for your patient explanation.

>This "error" is part of the "but don't remove the target directory
>itself" and for this purpose is entirely innocuous and expected.

Then that means there is no true drop-in for DELTREE under W2K. That's
what I wanted to know.

>I'd be careful with DELTREE since it is not supplied with W2K.

IMHO, that's not suffiicient reason to avoid it.

>It may fail under conditions found but for which it is was not
>designed.

Hasn't yet. (I've been using it on multiple systems for years.)

>RMDIR is "native".

.... and DELTREE "works".

>Enhance your batch usage by confirming the CD
>after you have changed into the target and before issuing
>RD /s /q .

I've got an even better idea. I'll keep using DELTREE. ;-)

Thanks again.

regards, Andy
--
**********

Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com

To identify everything that starts up with Windows, download
"Silent Runners.vbs" at www.silentrunners.org

**********

Bill Stewart
Andrew Aronoff wrote:

> Sorry, I wasn't clear. I wanted to know how to avoid the error, not
> suppress the display.

What's the difference?

--
Bill Stewart

Mark V
In microsoft.public.win2000.cmdprompt.admin Andrew Aronoff wrote:

> Thanks for your patient explanation.
>
>>This "error" is part of the "but don't remove the target
>>directory itself" and for this purpose is entirely innocuous and
>>expected.
>
> Then that means there is no true drop-in for DELTREE under W2K.
> That's what I wanted to know.

Correct. Nothing exactly equivalent is provided. But there are
likely 3rd-party "deltrees" written for NTx available.

>
>>I'd be careful with DELTREE since it is not supplied with W2K.
>
> IMHO, that's not suffiicient reason to avoid it.

Good luck.

>>It may fail under conditions found but for which it is was not
>>designed.
>
> Hasn't yet. (I've been using it on multiple systems for years.)
>
>>RMDIR is "native".
>
> ... and DELTREE "works".

As you like.

>>Enhance your batch usage by confirming the CD
>>after you have changed into the target and before issuing
>>RD /s /q .
>
> I've got an even better idea. I'll keep using DELTREE. ;-)

foxidrive
On Fri, 30 Sep 2005 15:25:48 +0200, Andrew Aronoff wrote:

> Thanks for your patient explanation.
>
>>This "error" is part of the "but don't remove the target directory
>>itself" and for this purpose is entirely innocuous and expected.
>
> Then that means there is no true drop-in for DELTREE under W2K. That's
> what I wanted to know.

Here ya go - better test it well.

@echo off
:: deltree.cmd
if [%1]==[] echo How about a folder, jerk!&goto :EOF
for /f "delims=" %%a in ('dir %1 /a:d /b') do rd /q /s %1\"%%a"
del /q %1\*.*

Jerold Schulman
On Thu, 29 Sep 2005 12:27:22 +0200, Andrew Aronoff wrote:

>I'm looking for a replacement for DELTREE.EXE under W2K. I've read in
>various Usenet posts that RD and/or DEL can be used, but I haven't
>found this to be true in my tests.
>
>I'm looking for a command to replace the following:
>
>DELTREE /Y C:\TEMP\
>
>This single command deletes the entire contents of the directory
>C:\TEMP (including subdirectories and their contents) _without_
>deleting the directory itself.
>
>RD /Q /S C:\TEMP deletes the TEMP directory, too.
>
>Is there a closer fit? (If not, I'll stick with DELTREE, which works
>perfectly well in a CMD.EXE shell under W2K and WXP.)
>
>regards, Andy


See tip 0617 » How do I remove all files and sub-directories from a folder, without removing the folder?
in the 'Tips & Tricks' at http://www.jsifaq.com


Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com

foxidrive
On Mon, 03 Oct 2005 09:24:59 -0400, Jerold Schulman wrote:

> On Thu, 29 Sep 2005 12:27:22 +0200, Andrew Aronoff wrote:
>
>>I'm looking for a replacement for DELTREE.EXE under W2K. I've read in
>>various Usenet posts that RD and/or DEL can be used, but I haven't
>>found this to be true in my tests.
>>
>>I'm looking for a command to replace the following:
>>
>>DELTREE /Y C:\TEMP\
>>
>>This single command deletes the entire contents of the directory
>>C:\TEMP (including subdirectories and their contents) _without_
>>deleting the directory itself.
>>
>>RD /Q /S C:\TEMP deletes the TEMP directory, too.
>>
>>Is there a closer fit? (If not, I'll stick with DELTREE, which works
>>perfectly well in a CMD.EXE shell under W2K and WXP.)
>>
>>regards, Andy
>
>
> See tip 0617 » How do I remove all files and sub-directories from a folder, without removing the folder?
> in the 'Tips & Tricks' at http://www.jsifaq.com

Not a good tip, Jerold. If %1 is invalid or a typo it will merrily delete
wherever it finds itself.

Andrew Aronoff
>See tip 0617 » How do I remove all files and sub-directories from a folder, without removing the folder?
> in the 'Tips & Tricks' at http://www.jsifaq.com

Tip 0617 might best be amended as follows:

To delete all the files and sub-directories in \My Test Folder
and leave an empty \My Test Folder, type:

DELTREE "\My Test Folder\"

To delete \My Test Folder, too, omit the trailing backslash:

DELTREE "\My Test Folder"

regards, Andy
--
**********

Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com

To identify everything that starts up with Windows, download
"Silent Runners.vbs" at www.silentrunners.org

**********

nospam.please@ualberta.ca
The following 4-line batch file is what I use (I name it emptytemp.cmd):

@echo off
if X%temp%==X goto :eof
rd/s/q %temp% 2>%temp%\delete.me
del %temp%\delete.me


Line 1: should be obvious
Line 2: if environment variable TEMP is not set, exits batch
(could be dangerous otherwise)
Line 3: removes the directory, redirecting error messages into a
file in the temp directory - this keeps the directory
itself from being deleted, as there is an open file in it
(also keeps you from seeing error messages)
Line 4: deletes the file containing error messages

You could hard-code the directory instead of using %temp%

wrote:

>I'm looking for a replacement for DELTREE.EXE under W2K. I've read in
>various Usenet posts that RD and/or DEL can be used, but I haven't
>found this to be true in my tests.
>
>I'm looking for a command to replace the following:
>
>DELTREE /Y C:\TEMP\
>
>This single command deletes the entire contents of the directory
>C:\TEMP (including subdirectories and their contents) _without_
>deleting the directory itself.
>
>RD /Q /S C:\TEMP deletes the TEMP directory, too.
>
>Is there a closer fit? (If not, I'll stick with DELTREE, which works
>perfectly well in a CMD.EXE shell under W2K and WXP.)
>
>regards, Andy

Andrew Aronoff
>The following 4-line batch file is what I use (I name it emptytemp.cmd):
>
>@echo off
>if X%temp%==X goto :eof
>rd/s/q %temp% 2>%temp%\delete.me
>del %temp%\delete.me

Excellent batch file. It empties the target directory of all files and
subdirectories without deleting the target directory itself. It
displays no warning or error messages. It does nothing if the target
directory doesn't exist.

Thanks very much for the tip.

It would also be a good idea to iterate with the following two
directories:

%SYSTEMROOT%\temp
"%USERPROFILE%\local settings\temp"

regards, Andy
--
**********

Please send e-mail to: usenet (dot) post (at) aaronoff (dot) com

To identify everything that starts up with Windows, download
"Silent Runners.vbs" at www.silentrunners.org

**********

Jerold Schulman
On Mon, 3 Oct 2005 23:33:14 +1000, foxidrive wrote:

>On Mon, 03 Oct 2005 09:24:59 -0400, Jerold Schulman wrote:
>
>> On Thu, 29 Sep 2005 12:27:22 +0200, Andrew Aronoff wrote:
>>
>>>I'm looking for a replacement for DELTREE.EXE under W2K. I've read in
>>>various Usenet posts that RD and/or DEL can be used, but I haven't
>>>found this to be true in my tests.
>>>
>>>I'm looking for a command to replace the following:
>>>
>>>DELTREE /Y C:\TEMP\
>>>
>>>This single command deletes the entire contents of the directory
>>>C:\TEMP (including subdirectories and their contents) _without_
>>>deleting the directory itself.
>>>
>>>RD /Q /S C:\TEMP deletes the TEMP directory, too.
>>>
>>>Is there a closer fit? (If not, I'll stick with DELTREE, which works
>>>perfectly well in a CMD.EXE shell under W2K and WXP.)
>>>
>>>regards, Andy
>>
>>
>> See tip 0617 » How do I remove all files and sub-directories from a folder, without removing the folder?
>> in the 'Tips & Tricks' at http://www.jsifaq.com
>
>Not a good tip, Jerold. If %1 is invalid or a typo it will merrily delete
>wherever it finds itself.


Thanks, fixed it:

@echo off
if {%1}=={} @echo Syntax: DelTree Folder&goto :EOF
if not exist %1 @echo Syntax: DelTree Folder - Folder %1 does not exist.&goto :EOF
pushd %1
if %ERRORLEVEL% NEQ 0 @echo Syntax: DelTree Folder - Folder %1 does not exist.&goto :EOF
del /q /f "*.*"
for /f "Tokens=*" %%i in ('dir %1 /B /A /AD') do rd /s /q "%%i"
popd

Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com

Bill Stewart
Andrew Aronoff wrote:

> > @echo off
> > if X%temp%==X goto :eof
> > rd/s/q %temp% 2>%temp%\delete.me
> > del %temp%\delete.me
>
> Excellent batch file. It empties the target directory of all files
> and subdirectories without deleting the target directory itself. It
> displays no warning or error messages. It does nothing if the target
> directory doesn't exist.

It's odd that you like the above solution, because to me you said:

> I wanted to know how to avoid the error, not suppress the display.

The script above is a less-elegant (IMO) version of this (which I posted
earlier):

pushd %1 || goto :EOF
rd /q /s . 2> NUL
popd

Either way, the rd command will generate an error because it won't
delete the current or an in-use directory.

--
Bill Stewart