Solved

batch file to delete all files and folders

Posted on 2008-10-15
8
1,906 Views
Last Modified: 2012-06-27
Here are the details:

1) The batch file sits in D:\
2) It needs to remove the contents of D:\test
3) D:\test must not be deleted, just its contents.
4) Its contents include dynamically named subfolders that contain files.

I need a batch to remove all of the files and folders regardless of read only, too big for recycle bin, etc.

Thanks.
0
Comment
Question by:hiltzj
8 Comments
 

Expert Comment

by:stedmunds
ID: 22720463
There are a few ways to do this.  The easiest way would be the following

rd d:\test /s /q
md d:\test

That would silently delete all subfodlers and files from d:\test folder including the test folder itself, then recreate the test folder.

There is a slightly more complex way of doing this that would keep the folder which I could go in to if you need.  But I feel this would be the best and easiest solution
0
 

Author Comment

by:hiltzj
ID: 22720498
the d:\test folder itself cannot be deleted, it needs to stay at all times. only the contents should be deleted.
0
 

Expert Comment

by:stedmunds
ID: 22720575
Then the more complex solution which I didn't have time to work out off the top of my head, so I stole it from the internet is:

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

Looking over the code, it seems right, but i would have a go yourself first with a seperate folder to test it
0
 

Author Comment

by:hiltzj
ID: 22720718
i ran that code on a sample dir named test.

D:\>test.bat

D:\>del d:\test\. /f /q
i" /s /q was unexpected at this time.

D:\>for /f "tokens=*" i" /s /q

D:\>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 4

Accepted Solution

by:
mctigue earned 250 total points
ID: 22721863
well in ms-dos you could use the deltree command, but otherwise here's what to do

:: Remove all files and subfolders but NOT the root folder
:: From tip 617 at JsiFAQ.com
@echo off
pushd %1
del /q *.*
for /f "Tokens=*" %%G in ('dir /B') do rd /s /q "%%G"
popd

I took that from http://www.ss64.com/nt/del.html
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 22722387
In a batch file you need double % for variables
del d:\test\. /f /q

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

Open in new window

0
 

Author Closing Comment

by:hiltzj
ID: 31506278
Perfect.
0
 

Expert Comment

by:stedmunds
ID: 22755972
Ahh I see.  I missed the 2nd % sign
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now