• Post Reply Bookmark Topic Watch Topic
  • New Topic

deleting files matches with given pattern in java  RSS feed

 
nagaraju dameruppula
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.File;

public class Filetest {
public static void main(String args[])
{
File file = new File("C:\\Example");
String filepattern="WS001";
String[] myFiles;
try{
if(file.isDirectory()){
myFiles = file.list();
for (int i=0; i<myFiles.length; i++) {
File myFile = new File(file, myFiles[i]);
if(myFile.getName().contains(filepattern))
{
myFile.delete();
}
}
}
}
catch(Exception e)
{

}
}
}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It helps tremendously if you do two things:

1) UseCodeTags (<--click that). I have re-posted your code with them here. See how much easier it is to read? Even if you don't think it is, this is how almost everyone else is used to seeing it, so THEY all find it easier to read.

2) You need to actually ask a question. What exactly are you having issues with? Nothing more than 25 lines of code in a post will simply be ignored by almost everyone.

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, for more generality you could create a FilenameFilter for use in that call to file.list()

See java.io.FilenameFilter interface.

Bill
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!