Results 1 to 7 of 7
  1. #1
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question How to delete files, based on a matching pattern in the filename in a directory.

    Hi

    Anyone pls tell me how to delete files for example having


    files names like:-
    12098_All_School_Reports.csv
    12367_All_School_Reports.csv
    :
    :

    I need to look for files having the pattern
    _All_School_Reports.csv and delete only those files from a directory.

    How to do that in java...

    Anyhelp is appreciated...
    Thank You

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to delete files, based on a matching pattern in the filename in a directory.

    A for loop that utilizes the String#contains(...) method should work nicely I think.

  3. #3
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default Re: How to delete files, based on a matching pattern in the filename in a directory.

    Sir

    i dont get Contains method.

    String deleteThisFile = "All_School_Reports.csv";
    Files[] files = location.listFiles();

    for (File file : files) {
    if(file.equals



    Can you pls send the code .
    Thank You.

  4. #4
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default Re: How to delete files, based on a matching pattern in the filename in a directory.

    File[] files = location.listFiles();

    for (File file : files)
    {
    if(file.toString().contains(deleteThisFile)) {
    file.delete();
    }
    // // Delete each file
    // if (!file.delete())
    // {
    // // Failed to delete file
    // System.out.println("Failed to delete "+file);
    // }
    }


    Thank You Sir, i got this to work.Thanks You.

  5. #5
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: How to delete files, based on a matching pattern in the filename in a directory.

    Don't use file.equals. What I would do is have a String variable called ending with a value of "_All_School_Reports.csv", and, inside the for loop:

    Java Code:
    if(file.length()>=ending.length()){
        if(file.substring(file.length()-ending.length()).equals(ending)){
            //whatever you want to do with the file
        }
    }
    edit: oops, forgot the toString()s after each case of file.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,340
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to delete files, based on a matching pattern in the filename in a directory.

    Quote Originally Posted by DiamondSoul View Post
    Don't use file.equals. What I would do is have a String variable called ending with a value of "_All_School_Reports.csv", and, inside the for loop:

    Java Code:
    if(file.length()>=ending.length()){
        if(file.substring(file.length()-ending.length()).equals(ending)){
            //whatever you want to do with the file
        }
    }
    edit: oops, forgot the toString()s after each case of file.
    That's what the contains( ... ) method is for (as already suggested).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: How to delete files, based on a matching pattern in the filename in a directory.

    Quote Originally Posted by DiamondSoul View Post
    Don't use file.equals. What I would do is have a String variable called ending with a value of "_All_School_Reports.csv", and, inside the for loop:

    Java Code:
    if(file.length()>=ending.length()){
        if(file.substring(file.length()-ending.length()).equals(ending)){
            //whatever you want to do with the file
        }
    }
    edit: oops, forgot the toString()s after each case of file.
    Just as an observer trying to figure this out...
    So if I had this: 12098_All_School_Reports.csv =28 in length, and ending being this: _All_School_Reports.csv = 23 in length

    Performing the first if would result in 28>=23 true, so then the second if would produce _All_School_Reports.csv.equals(ending)...which is true.
    Last edited by kraigballa; 10-11-2011 at 10:24 PM.

Similar Threads

  1. A Question Regarding Pattern Matching
    By hadi in forum Advanced Java
    Replies: 4
    Last Post: 01-21-2011, 03:38 PM
  2. pattern matching with two arrays
    By jessie in forum New To Java
    Replies: 10
    Last Post: 11-24-2010, 04:00 PM
  3. String matching a pattern
    By vividcooper in forum New To Java
    Replies: 7
    Last Post: 01-07-2010, 10:30 PM
  4. Reading a directory and getting the filename
    By mrjunsy in forum Advanced Java
    Replies: 1
    Last Post: 05-10-2008, 02:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •