Java: Deleting files that start with a specific string.

Posted on 2006-06-04
Last Modified: 2010-03-31

What would be the most efficient way of deleting files from a directory that start with a specific string.   For example:

String startString = “300”;

Than all files that start with 300 and l300 (that's lower "L" and 300) will be deleted.

Question by:kasiencja
    LVL 26

    Expert Comment

    1. get the list of all files from the dir
    2. loop through each file and check the file name starts with the string or 'l' + string
    3. true --> delete it
    4. false --> continue

    Author Comment

    I was hoping I could use something like FilenameFilterEx, however through trial and error I found out that the filter only works for file extensions.
    LVL 15

    Expert Comment

    I tested this and it seems to do what you're asking:


    public class dir {
          static private String[] files;
          static File dir=new File("./");
          public static void main(String[] args) {
                for(int i=0;i<files.length;i++){
                            File temp=new File(files[i]);
                            System.out.println("Deleted: "+files[i]);
    LVL 15

    Expert Comment

    That's for the current directory (./), and I guess the .toString() is unnecessary...
    LVL 15

    Expert Comment

    I didn't really think about why you would want to do this, but it would be much more efficient to weite a shell (or batch) script to do this at the os level.
    LVL 27

    Accepted Solution

    Try something like  
        FilenameFilter deleteFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.startsWith("300") || name.startsWith("l300");
        File dir = new File(pathToDirectory);
        File[] dFiles = dir.listFiles(deleteFilter);
        for(int i =0; i< dFiles.length;i++){
                if(dFiles[i].delete()) System.out.println(dFiles[i].getName() + " was deleted");
                  else System.out.println("could not delete " + dFiles[i].getName());

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now