Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

batch file to delete all files and folders

Posted on 2008-10-15
8
Medium Priority
?
1,927 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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

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.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

597 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