aspose file tools*
The moose likes Java in General and the fly likes Delete multiple files with the same prefix string in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Delete multiple files with the same prefix string in java" Watch "Delete multiple files with the same prefix string in java" New topic
Author

Delete multiple files with the same prefix string in java

Mark Butcher
Ranch Hand

Joined: Jun 28, 2012
Posts: 46
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

Have you looked at the API for File? It has some methods which will help you do what you want.


Steve
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mark Butcher
Ranch Hand

Joined: Jun 28, 2012
Posts: 46
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

Joined: Jan 10, 2002
Posts: 61198
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark Butcher
Ranch Hand

Joined: Jun 28, 2012
Posts: 46
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

Joined: May 30, 2012
Posts: 679
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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Delete multiple files with the same prefix string in java