Saturday, 14 September 2013

deleting the particular type of zip files in a target folder if they are created before seven days

deleting the particular type of zip files in a target folder if they are
created before seven days

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 ,so the target folder
will be having different types of files now if I talk of zip files in the
target folder then there must be different types of zip files of different
name but my point is that I have to look the zip files which name is
starting with pattern abcd so I have to look the zip files of which name
is starting with 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 the 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);
}

No comments:

Post a Comment