Question:
Simple batch file to remove non empty folders?
biggestperlnerd
2007-10-25 08:31:46 UTC
I need a simple batch file to work on XP that will go into a specific directory and delete all files and sub folders (including non empty directories).

Below is what I had and I thought it worked, but it deletes the root folder too. I need to keep the initial folder and delete everything else.

@ECHO OFF
rmdir "C:\test" /s

echo C:\test has been deleted successfully
echo Please inform the ISD technician that the system is ready

pause

Secondly, is there a way to make it NOT prompt for y or n on deletions? The less intervention of our endusers, the better.
Five answers:
Kevin
2007-10-25 17:23:52 UTC
There's a couple ways to approach this.



Option #1) The easiest way.

If you just want a fresh, empty "c:\test" directory, then you could simply delete the whole thing, then create a new directory. You were on the right track for this ... just needed the "/q" for quiet deletion, and recreate your test directory.





@echo off

rd c:\test /s /q

md c:\test



echo C:\test has been deleted successfully

echo Please inform the ISD technician that the system is ready

================





Option #2) A little more complex.

You can parse the files and folders in the c:\test directory and delete them from the root.





@echo off

rem delete files quietly from the root of c:\test.

del c:\test\. /f /q



rem delete all sub-dirs quietly from the root of c:\test

for /f "tokens=*" %i in ('dir test /b') do rd "%i" /s /q



echo C:\test has been deleted successfully

echo Please inform the ISD technician that the system is ready



==============

Couple of other notes on other suggestions,



Deltree - no longer native in Windows 2000 or Windows XP. Retired with NT/95.



Doing del c:\test\*.* /(whatever the switch is), plus the switch for not prompting,

Which would look like,



del c:\test\*.* /s /q



Would delete all non-readonly files, but not sub-directories. You'd be left with whatever directory tree structure you had under c:\test, plus any read-only files that might be there.



==============



I'd go for option #1, personally.



==============
nyman
2016-10-18 04:11:14 UTC
Batch Delete Folder
donnellan
2016-12-26 15:33:38 UTC
Delete Non Empty Directory
DynaSoar
2007-10-25 10:00:30 UTC
I'm not entirely sure in windows but in DOS I would try "DELTREE /y" to delete the entire directory with out fighting the "y/n prompt" issue and then add a "MKDIR DirectoryName" command in the BAT file.
2007-10-25 08:50:01 UTC
Check to options to del, I don't have a Windows machine here to check, but if you type del /? from a command prompt, I think there is a switch to make it do a recursive delete. Then use :

del c:\test\*.* /(whatever the switch is), plus the switch for not prompting. Use with care.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...