File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "delete files in a driectory" Watch "delete files in a driectory" New topic

delete files in a driectory

Kiran Gaddam
Ranch Hand

Joined: Aug 05, 2011
Posts: 35
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..

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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.
I agree. Here's the link:
subject: delete files in a driectory
jQuery in Action, 3rd edition