?
Solved

batch file to delete all files and folders

Posted on 2008-10-15
8
Medium Priority
?
1,925 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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
 
LVL 4

Accepted Solution

by:
Stephen McTigue earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

777 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