DELTREE equivalent

  • Thread starter Thread starter Andrew Aronoff
  • Start date Start date
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 <Drive:>\My Test Folder
and leave an empty <Drive:>\My Test Folder, type:

DELTREE "<Drive:>\My Test Folder\"

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

DELTREE "<Drive:>\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

**********
 
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%
 
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

**********
 
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
 
Andrew said:
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.
 
Back
Top