• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to delete all existing files in a dierectry?  RSS feed

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody!

I want to write a file to a directory / folder.
Before doing it,
I want to delete all existing files in that particular folder.
Can anyone please let me know how to do it?

Regards,
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can use File.list() method to list all the filenames in a dir and then file.delete to delete the same.
Sample code is given below.



regards
Sri
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks SriRam,
That was exactly what i wanted. My problem is solved.

Thanks
 
Hemal Pandya
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ashvini Devi:
Thanks SriRam,
That was exactly what i wanted. My problem is solved.

Thanks


Note that File.delete returns a boolean indicating success, which you may want to attend to. Also, can the directory contain other directories? In that case, recursion will be required.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!