This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes delete files in a driectory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "delete files in a driectory" Watch "delete files in a driectory" New topic
Author

delete files in a driectory

Kiran Gaddam
Ranch Hand

Joined: Aug 05, 2011
Posts: 35
    
    1
Hello All,

I need to write code to delete all the files in a specified directory, but not the directory..

I have this code...



but it is deleting the whole folder and files in it ( "C:/Users/aaaa/Desktop/asdf").
I only want the files in the folder to be deleted... not the folder

any ideas is appreciated..

Thankyou
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19693
    
  10

Well, yes, that's what line 22 does. So apparently you want the starting directory (your "specified" directory) to be treated differently than the directories under it. The starting directory should be left alone but the directories under it should be deleted.

Everything else in your code looks just fine: recursive method (good), don't delete directory until everything in it has been deleted (good), but unfortunately you treat the starting directory the same as the others (not good). So you're going to need additional code which takes care of that particular situation.
 
Consider Paul's rocket mass heater.
 
subject: delete files in a driectory
 
It's not a secret anymore!