• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Delete multiple files with the same prefix string in java  RSS feed

 
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
 
Bartender
Posts: 4179
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.
 
Bartender
Posts: 10575
66
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.
 
Author and ninkuma
Marshal
Posts: 66823
168
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.
 
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 ?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!