Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Delete multiple files with the same prefix string in java

 
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 Python Java
  • 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: 10777
71
Hibernate Eclipse IDE 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.
 
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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 ?
 
I am going to test your electrical conductivity with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic