tuntun wretee
tuntun wretee

Reputation: 49

deleting zip files of certain pattern type

I have written a condition in which it has to look up in the target folder and have to delete the file which os of type .zip extension and those also which are beginning with the name like 'abcd_edf' pattern that is file name could abcd_edf_tyu454656.zip but also it has to make sure that such zip files are not created seven days before if they are beging with the pattern 'abcd_edf' and is of type zip and is created seven days before then it should be get deleted below is the solution with which I came but it is not working please advise what is wrong in it..

if (c.isDirectory()) {
                final File[] files = c.listFiles();
                long currentTime = System.currentTimeMillis();

                for (int i = 0; i < files.length; i++) {
                    //if (currentTime - files[i].lastModified() > 1000 * 60 * 60* 24 * 7)
                      String fileName = files[i].getName();

                           if(fileName.matches("abcd_edf*\\.zip") && currentTime - files[i].lastModified() > 1000 * 60 * 60* 24 * 7) 
                           {
                                                       files[i].delete();
                           }

                }

            }

Upvotes: 0

Views: 860

Answers (2)

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

Adjust the regex to match the expression:

if(fileName.matches("abcd_edf.*\\.zip")

abcd_edf matches string literal

.* matches 0 to n occurrences of any character

\\.zip matches literal .zip

Currently the regex is looking for 0 to n occurrences of f however it should be looking for 0 to n occurrences of any character, which is denoted by .. The quantifier * is being applied to the preceding character in the current expression, causing the issue. Pay close attention to the second test, which passes illustrating the behavior of the current regex.

public static void main(String[] args) {
    String fileName = "abcd_edf.zip";
    String fileName1 = "abcd_edfffffff.zip";
    String fileName2 = "abcd_edf_somethingelse.zip";



    System.out.println(fileName.matches("abcd_edf*\\.zip")); //prints true
    System.out.println(fileName1.matches("abcd_edf*\\.zip")); //prints true
    System.out.println(fileName2.matches("abcd_edf*\\.zip")); //prints false

    System.out.println(fileName.matches("abcd_edf.*\\.zip")); //prints true
    System.out.println(fileName1.matches("abcd_edf.*\\.zip")); //prints true
    System.out.println(fileName2.matches("abcd_edf.*\\.zip")); //prints true
}

Upvotes: 2

Ilya
Ilya

Reputation: 29703

You should add braces after if with time check

final long week = 1000 * 60 * 60* 24 * 7;
for (int i = 0; i < files.length; i++) 
{
   if (currentTime - files[i].lastModified() > week )
   {
       String fileName = files[i].getName();
       if(fileName.matches("abcd_edf*\\.zip")) 
       {
            files[i].delete();
       }
   } 
}

Upvotes: 2

Related Questions