• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Delete multiple files with the same prefix string in java

 
Mark Butcher
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Want to delete all files starting with a particular string pattern in a folder using java.
For example, there may be multiple files containing prefix "abc" like :
abc.txt
abc.xls
abc.doc
abc.xlsx

I want to delete all of the above files starting with "abc" irrespective of their extensions.

The only thing I know is to delete one single file as follows :

Thanks
Mark
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the API for File? It has some methods which will help you do what you want.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Butcher wrote:The only thing I know is to delete one single file as follows :...

Actually, that won't just delete files; it'll delete directories as well (or it may do), because a File can be either an actual file OR a directory.

I recommend that you study the File class documentation (java.io.File (←click)) carefully, because all the methods you need are contained in it. A look at the tutorials probably wouldn't go amiss either, because it also covers some of the java.nio.file package features (new with version 7) - in particular the Files class.

HIH

Winston
 
Mark Butcher
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I am using java 6 and didnt find anything helpful in this context.

Atleast listing files with a particular prefix string would really help me.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what you want to do is to list (cough, cough) files based upon a filter (nudge, nudge), in this case, all that possess a common prefix.
 
Mark Butcher
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

If I create files prefixing my name as an identifier, lets say Mark1.xls, Mark2.xls,.......
These files belong to me and I can manipulate only those.
Now I want to delete only those files created by me, that is Mark1.xls to MarkN.xls

Thanks.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Steve told you what class to use and Bear has hinted heavily at what methods of that class you should use, so what problems are you still having ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic