Solved

batch file to delete all files and folders

Posted on 2008-10-15
8
1,920 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
DevOps Toolchain Recommendations

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

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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