Batch Scripting - Delete all Directories within a Directory

madmatt

Awesome is as awesome does.
Political Access
Joined
5 Apr 2002
Messages
13,314
DEL only deletes files
RD not only deletes the contents of a directory but the directory itself

I need a command that will delete only the contents (including other directories) of a directory.

I don't know all the directory names within this directory so it needs to do the work for me.

Help ASAP is appreciated. Thank you.
 
del should delete directories as welll :s

del *.*/s would delete all files and subdirectories
 
It doesn't. It only deletes the contents of those directories and not the directories.
 
RD will delete the base directory. I only want to delete sub directories.

e.g.

C:\Work
C:\Work\Temp1\
C:\Work\Temp2\
C:\Work\filename.txt

I want to delete everything EXCEPT C:\Work
 
so wouldn't deltree c:\work\Temp1 work? then just do the same for the others. i don't think there is a way to specify deleting all the subdirectories within work all together.

or you could do this

DELTREE /Y c:\work\temp1 c:\work\temp2 del c:\work\filename.txt

i think thats it, maybe wrong someplace.
 
Last edited:
Install unix services for Windows and use bash to write a shell scripts which does this for you:

rm -rf *

Done :D
 
Deltree is obsolete because the RMDIR command now includes the /S option which removes folders that aren't empty. Combine it with the /Q and you won't even be prompted to confirm.

I could probably write something using the FOR command, I'll have to look into it later as my lunch is almost over :D

-EDIT-

Ok something quick I slapped together....

Code:
RD C:\TEST\TEST\NEW /s /Q

Put that in a batch file, it will go down to the C:\TEST\TEST\ directory and delete the "NEW" folder.

This could be tweaked, so that it skips down to your potential directories, such as c:\work\ and then afoheaofheaiohfe

OK, edit again :D

I just tested it and try this :D

Code:
@ECHO OFF
 
RD C:\WORK\ /s /Q

That will delete all files and directories including the work folder

I'll play later, but you are close.

I suppose you could do something like this...

Code:
RD C:\Work\Folder1 /S /Q
RD C:\TEST\Folder2 /S /Q

Just let the commands run, using the /S and /Q switches appropriately
 
Last edited:
Yes, DELTREE is obsolete.

RM/RMDIR would work but it deletes the base directory which is bad.
 
^^^Scared the crap outta me when I was testing, I had Windows Explorer in one of those directories and then all the sudden it changed to c:\windows :eek:

I was like - crap!

OK, this works for any and all subfolders, changing the path and folder names appropriately -

Code:
@ECHO OFF
RD C:\TEST\test1 /s /Q
RD C:\TEST\test2 /s /Q
RD C:\TEST\test3 /s /Q

As long as there are only folders in the root you want to keep, and not files, that should do it?

If there are files, I think you could just add a DEL *.* on there from say -

DEL C:\TEST *.*

But if you force to directories, you can mix and match commands.

Use a DEL in the root folder for any files that may be there,
 
I don't know the names of the directories within "C:\TEST\".
 
Ah, I can see how that would put a wrinkle in things.

Let me keep fiddling.....
 
give me a couple minutes.. i'll whip up something for you

edit: here ya go
Code:
@echo off
dir /b /ad %1 > dirlist.txt
for /f %%i in (dirlist.txt) do rd /s /q %1\%%i
del %1\*.*
del dirlist.txt
Save it to a batch file.. run it from the command line and pass the base directory to it as a parameter.

Example:
Let's say the batch file is called.. "delsub.bat" and it's saved to the root of the C: drive.. and you have a directory c:\work that you want to delete all the sub directories from.. you would do as follows:

c:\> delsub c:\work

IMPORTANT: you shouldn't add a trailing "\" to the directory name you pass.

edit2:
If you want to know how it works..
@echo off
Hopefully you know what this does..

dir /b /ad %1 > dirlist.txt
Creates a file called dirlist.txt in the current working directory that contains a list of all the the subdirectories of the base directory. The "/b" does a bare listing (only the names.. none of the dates, etc). The "/ad" switch just lists the directories. The "> dirlist.txt" portion redirectes the output of the command to a file called dirlist.txt

for /f %%i in (dirlist.txt) do rd /s /q %1\%%i
Loops through the dirlist.txt file reading the directory names and runs the command "rd /s /q <directory name> for each subdirectory listed in the file. I won't go into all the switches available with the for command.. running "for /?" from the command prompt will display several pages of info.

del %1\*.*
Deletes all the files from the base directory

del dirlist.txt

Just does a little cleanup and deletes the dirlist.txt file it created. You can comment/delete this line out if you to retain the list of directories that were deleted.

edit3: made some changes because I noticed in an earlier post that you did want to delete the files out of the base directory as well.
 
Last edited:
I knew the for command was the way to go, I'm such a newb :(
 
Thank you fitz... cookies on the way!
 
Instead of looping through all of the subdirectories, how about this:

RD %1\ /s /q
MD %1\

Just remove the parent directory and it's contents, then add just the parent directory back. But I don't know if there is a permission issue when the parent directory is a system directory or not.
 
That would probably work..

but .. what if you wanted to delete all the directories on a network share.. and the network share is your root? you couldn't do a:

rd /s /q \\server1\sharename

but using the batch file with a loop would work.. okay.. i'm making excuses for why my way is better :)

edit: oh! oh! and what if you wanted to keep the files in the base directory and just delete all the sub directories.. then my way is better because you could remove the line to delete the files!
 
Your way is better, I have no doubt - I was just playing with it and found that way to work too.

Your way is better because my way can't be used in the event that the parent folder is a system folder - you would receive access denied errors :(

But Hey, I'm just an apprentice compared to you - I'm learning :)
 
lol.. i'm always learning new things :)

Your way is rather simple and would work for most situations (the KISS method is always a good thing!) and it does show some imagination/troubleshooting ability that I often find lacking in many people who work in IS/IT and claim to be IS/IT professionals.

I find it is not necessary how much you know, but the ability to learn and approach problems from different ways that make one person a better tech than another.

Ok.. off my soapbox now..
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back