Link to home
Start Free TrialLog in
Avatar of Zeman4323
Zeman4323

asked on

Batch File - Delete Directory Contents

I want to make a batch file that basically does a rmdir.  However I want only the directories contents and subdirectories to be deleted, not the main directory itself.  Does any one know if this is possible, and if so how to do it?

Thanks

Matthew
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Try this:

@echo off

for /f "delims=" %%a in ('dir /b /ad 2^>NUL') do echo rd /s /q "%%a"

Run this from the directory where you want to delete subdirecties. Note, I've placed an echo prior to the remove directory (rd) command for testing. To actually do the processing you'll want to remove the echo statement.

Good Luck,
Steve
Avatar of Zamba1
Zamba1

Shorter:

del *.* /s /q

will delete all files and all subdirectories of the current directory, but not the directory itself.
Only problem is that command won't remove the subdirectories.
oops ... you're right - that was submitted too fast.
Avatar of Zeman4323

ASKER

hey thanks for the imput.  I cant try it tonight, but I will tomorrow and Ill give you guys some feedback.  THanks Again
When I try doing this:
for /f "delims=" %%a in ('dir /b /ad 2^>NUL') do echo rd /s /q "%%a"

I get %%a unexpected at this time.

Sorry, but I was wondering if you knew what is going wrong.  
Thanks.

Also, Sorry for the trouble, and that it took me so long to get back to you.  I had a busy weekend.  However, I did add 20 more points to this question.  I would add some more, but 20 is all I have left

Well, Thanks Again
Very easy - try with this batch.

You should create a DC.BAT batch file (DC=Delete contents).

This version can delete content of any folder. The Syntax is

dc c:\Folder1\folder2\folder3  (and only the contents of folder3 is to be deleted)

In this File you should  copy next commands:

cd %1
cd..
rd %1 /s /q
md %1
cd %1

The last Line is optional and you can avoid it


A simplified Version is

rd %1 /s /q
md %1

but you should be in root of destination folder.

Example:
You should be in c:\Folder1\Folder2, and you can type:

DC Folder3

This batchfile can both be run commandline with the foldername mentioned after filename (in quotation marks) or run without arguments to have it prompt you for the foldername.

I warn you that I have not tested it and claim no responsibility for whatever may come from using it. The responsibility is yours.

This is effectively zvonkodj's batchfile except prettier and with a few "just in case" features. I only noticed he came up with it after I wrote this file because I refreshed the original page (so we've both independently come up with the same solution). You need to run it in the root folder.

@ECHO OFF
SETLOCAL
ECHO Please note that this program is intended to run from the folder outside the one you want to delete.
SET CLNDIR=None.
IF NOT "%~1"=="" SET CLNDIR=%~1
IF "%~1"=="" SET /P CLNDIR=Enter Directory Name:
SET CLNDIR=%CLNDIR:"=%
IF EXIST "%CLNDIR%" GOTO :EXISTS
ECHO Error: Directory "%CLNDIR%" does not exist.
GOTO :EOF
:EXISTS
RD /S /Q %CLNDIR%
MD "%CLNDIR%"
The following section:

SET CLNDIR=%CLNDIR:"=%
IF EXIST "%CLNDIR%" GOTO :EXISTS

Should be:

SET CLNDIR=%CLNDIR:"=%
IF NOT "%CLNDIR%"=="None." IF EXIST "%CLNDIR%" GOTO :EXISTS
Caudax: You posted your original code on 7/12 and zvonkodj and I created our code on 7/5. You were sitting on this page for 7 days and constructed the code...

Zeman4323: I didn't see your response regarding the problem running my code from the command line. To do this you must only use one percent. My code was meant to be run from a batch file (.bat):

for /f "delims=" %a in ('dir /b /ad 2^>NUL') do echo rd /s /q "%a"

That will do the trick from the command line. But it's much easier to create a batch file named DeteleSubs.bat:

@echo off

for /f "delims=" %%a in ('dir /b /ad 2^>NUL') do echo rd /s /q "%%a"

ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry it took me so long, thanks a lot
SteveGTR: Don't accuse me of being an [expletive]; at any rate, I did not see his code until I refreshed the original page by which time I had already coded my own.