0

I was moving a zip file from source directory to target directory but since the target directory may also contain some other files like text files , image , documents files , zip files also.

I have to look and track the zip files whose name is starting with pattern abcd and after that it could be anything like abcd4567fg.zip so I have to catch such zip files and also have to look their creation time since my ultimate goal is not to keep such zip files whose name is starting with abcd in the target directory if they are created before seven days , I have come up with the below solution but it is not appropriate please advise

long  timeInEpoch = System.currentTimeMillis(); // slightly faster than new Date().getTimeInMillis();
File f = new File("/tmp");
if (f.isDirectory()) {
    final File[] files = f.listFiles();
    for(int i =0; i < files.length ; i++ ) {
       if( timeInEpoch  - f.lastModifiedDate()  > 1000*60*60*24*7 )  
           files[i].delete();
    }
    System.out.println(fileList);
}
tuntun wretee
  • 49
  • 1
  • 6
  • Do you know what _punctuation_ is? It's hard to read your question. Anyway, _I have come up with the below solution but it is not appropriate_, can you explain why? – BackSlash Sep 14 '13 at 08:17
  • I will edit it , the solution is not appropriate since it will delete all the files in that folder whether it is text file or image file and it will also delete the other zip files whose name is not started with abcd which I don't want – tuntun wretee Sep 14 '13 at 08:18
  • @tuntunwretee: So your question is *really* only about "how can I detect files starting with "abcd" and ending in ".zip"? If you've done the time-based part, that's all you need, surely. – Jon Skeet Sep 14 '13 at 08:25
  • Because of the parcularities with time, I would be tempted to convert the files last modified date to a Date object and compare it to matcher Date that is 7 days in the past, it Thats just me ;) – MadProgrammer Sep 14 '13 at 08:43
  • @jon yeah perfect that is my question – tuntun wretee Sep 14 '13 at 08:45

2 Answers2

0

You have to check if the file name starts with abcd and ends with .zip, so

long  timeInEpoch = System.currentTimeMillis(); // slightly faster than new  Date().getTimeInMillis();
File f = new File("/tmp");
if (f.isDirectory()) {
    final File[] files = f.listFiles();
    for(int i =0; i < files.length ; i++ ) {
       String fileName = files[i].getName():
       if(fileName.startsWith("abcd") && fileName.endsWith(".zip") && timeInEpoch  - f.lastModifiedDate()  > 1000*60*60*24*7)  
           files[i].delete();
    }
    System.out.println(fileList);
}

You can also use a RegEx:

if(fileName.matches("abcd.*\\.zip")) //file name starts with abcd and ends with .zip

But i think it would be slower


Anyway, one could simply rename a .jpg file to .zip, so if you really want to know if your file is a zip or not you should check its MIME

Community
  • 1
  • 1
BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • 1
    @bsd No. The `matches` method, matches the full string, so the start and end identifiers (`^` and `$`) aren't needed – BackSlash Sep 14 '13 at 08:34
  • @BackSlash see filename is abcd_fgfh_fhjdfjd_ty.zip so can i go for regular expression like this ..if(fileName.matches("abcd*\\.zip")) – tuntun wretee Sep 14 '13 at 08:47
0
    String fileStartsWith = "abcd";
    String fileExtention = ".zip";
    String folderLocation = "/temp";
    long lValueOF7days = 7 * 24 * 60 * 60 * 1000l;
    long timeInEpoch = System.currentTimeMillis(); // slightly faster than new Date().getTimeInMillis();

    File f = new File(folderLocation);
    if (f.isDirectory()) {
        final File[] files = f.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].getName().startsWith(fileStartsWith) && files[i].getName().endsWith(fileExtention) && (timeInEpoch - f.lastModified()) > lValueOF7days) {
                System.out.println("File " + files[i].getName());
                files[i].deleteOnExit();
            } else {
                System.out.println("Dont do any operation on other files :: " + files[i].getName());
            }
        }
    }
Satyam Koyani
  • 4,236
  • 2
  • 22
  • 48