Solved

batch file to delete all files and folders

Posted on 2008-10-15
8
1,901 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

24 Experts available now in Live!

Get 1:1 Help Now