Java - Do not delete files that match any of the array values - java

I have a list of files(approximately 500 or more files) where the filename contains a date.
file_20180810
file_19950101
file_20180809
etc.
What I want to do is delete files which exceed the storage period.
I've come up with the following logic so far
~Get dates of valid storage period (ie. if storage period is 5 days and date today is 20180810, store date values 20180810, 20180809, 20180808, 20180807, 20180806, 20180805 in an array.
~Check every file in a directory if it contains any of the following dates. If it contains date, don't delete, else delete.
My problem here is, if the file name does contain one single date and I use a loop to delete a file, it might delete other files with valid dates as well. To show what I want to do in code form, it goes somehow like this:
if (!fileName.contains(stringDate1) &&
!fileName.contains(stringDate2) &&
!fileName.contains(stringDate3)) //...until storage period
{//delete file}
Is there a better way to express this? Any suggestions for a workaround?
Please and thank you.

Parse dates from your filename. Here's an example:
import java.time.*;
import java.util.regex.*;
public class MyClass {
public static void main(String args[]) {
LocalDate today = LocalDate.now();
long storagePeriod = 5L;
String fileName = "file_20180804";
int year = 0;
int month = 0;
int day = 0;
String pattern = "file_(\\d{4})(\\d{2})(\\d{2})";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(fileName);
if (m.find()) {
year = Integer.parseInt(m.group(1));
month = Integer.parseInt(m.group(2));
day = Integer.parseInt(m.group(3));
}
LocalDate fileDate = LocalDate.of(year, month, day);
if (fileDate.isBefore(today.minusDays(storagePeriod))) {
System.out.println("Delete this file");
}
}
}

You can try using Regex to extract the actual date of each file and check for the inclusion in a validity period.
Pattern p = Pattern.compile("file_(?<date>\d{6})");
foreach(File f : filelist){
Matcher m = p.matcher(f.filename());
if(m.find()){
Date fileDate = new Date(m.group("date"));
if(fileDate.before(periodStartDate)){
file.delete();
}
}
}
The code is not precise and should not compile, check about Date object creation and comparison, but the main idea is pretty much here.

You can only delete Files that are not in the Array like (tested, working):
String path = ""; // <- Folder we want to clean.
DateFormat df = new SimpleDateFormat("yyyyMMdd"); // <- DateFormat to convert the Calendar dates into our format.
Calendar cal = Calendar.getInstance(); // <- Using Calendar to get the days backwards.
ArrayList<String> dr = new ArrayList<String>(); // <- Save the dates we want to remove. dr = don't remove
dr.add(df.format(cal.getTime())); // <- add the actual date to List
for(int i = 0; i < 5; i++) { // <- Loop 5 Times to get the 5 Last Days
cal.add(Calendar.DATE, -1); // <- remove 1 day from actual Calendar date
dr.add(df.format(cal.getTime())); // <- add the day before to List
}
for(File file : new File(path).listFiles()) { // <- loop through all the files in the folder
String filename = file.getName().substring(0, file.getName().lastIndexOf(".")); // <- name of the file without extension
boolean remove = true; // <- Set removing to "yes"
for(String s : dr) { // <- loop through all the allowed dates
if(filename.contains(s)) { // <- when the file contains the allowed date
remove = false; // <- Set removing to "no"
break; // <- Break the loop for better performance
}
}
if(remove) { // <- If remove is "yes"
file.delete(); // <- Delete the file because it's too old for us!
}
}
but this is not the best way! A much better method would be to calculate how old the files are. Because of the _ you can pretty easily get the dates from the filenames. Like (not tested):
String path = ""; // <- Folder we want to clean.
Date today = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMdd"); // <- Dateformat you used in the files
long maxage = 5 * 24 * 60 * 60 * 1000; // <- Calculate how many milliseconds ago we want to delete
for(File file : new File(path).listFiles()) { // <- loop through all the files in the folder
String fds = file.getName().split("_")[1]; // <- Date from the filename as string
try {
Date date = df.parse(fds); // Convert the string to a date
if(date.getTime() - today.getTime() <= maxage) { // <- when the file is older as 5 days
file.delete(); // <- Delete the file
}
} catch (ParseException e) {
e.printStackTrace();
}
}

Here is some example code which demonstrates how a list of input files (file name strings, e.g., "file_20180810") can be verified against a supplied set of date strings (e.g., "20180810") and perform an operation (like delete the file) on them.
import java.util.*;
import java.io.*;
public class FilesTesting {
private static final int DATE_STRING_LENGTH = 8; // length of 20180809
public static void main(String [] args) {
List<String> filter = Arrays.asList("20180810", "20180808", "20180809", "20180807", "20180806", "20180805");
List<File> files = Arrays.asList(new File("file_20180810"), new File("file_19950101"), new File("file_20180809"));
for (File file : files) {
String fileDateStr = getDateStringFromFileName(file.getName());
if (filter.contains(fileDateStr)) {
// Do something with it
// Delete file - if it exists
System.out.println(file.toString());
}
}
}
private static String getDateStringFromFileName(String fileName) {
int fileLen = fileName.length();
int dateStrPos = fileLen - DATE_STRING_LENGTH;
return fileName.substring(dateStrPos);
}
}

If you’re using ES6 you can use array includes and return a true or false to validate.
['a', 'b', 'c'].includes('b')

Related

Why after split line into an array, the code after for-loop doesn't work?

In this cycle, the string within an arraylist is divided, and each word is inserted into an array. I only need the date that is in second position to compare it with a date that I pass. If it is not present, the entire string is deleted from the arraylist.
For this reason I use an iterator. Everything works but the code after the for loop doesn't work. Eliminating the insertion of words in the array works everything. I used the same method elsewhere and it works without problems, I don't understand.
CreateMap(ArrayList<String> eventi,String data) throws ParseException {
list_eventi = eventi;
intervallo = data;
String [] periodo;
String[] arrayData;
periodo = intervallo.split("-");
String data_in = periodo[0];
String data_fin = periodo[1];
SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy");
Date dateIn = format.parse(data_in);
Date dateFin = format.parse(data_fin);
String[] line;
for (Iterator<String> iter = eventi.listIterator(); iter.hasNext(); ) {
String a = iter.next();
line = a.split(" "); //this is the problem//
String d = line[2];
Date dateImport = format.parse(d);
if(!dateImport.before(dateIn) && !dateImport.after(dateFin)) {
//date che sono nell'intervallo
// date between
System.out.println(d);
} else{
System.out.println("dati da eliminare " + a);
//iter.remove();
}
}
------------------------------ after this line the code doesn't execute
System.out.println("dati rimanenti");
System.out.println(list_eventi.toString());
//Map_for_login(eventi);
//Map_for_all(eventi);
There is no error message after executing the code, but after the for loop there are other methods and various system.out but they don't work
Since your variable eventi is an ArrayList you can just use the .forEach() method.
Fix:
List<String> resEventi = new ArrayList(); // the resulting eventi arrayList with only the correct values
eventi.forEach( a -> {
line = a.split(" ");
String d = line[2];
Date dateImport = format.parse(d);
if(!dateImport.before(dateIn) && !dateImport.after(dateFin)) {
//date che sono nell'intervallo
// date between
resEventi.add(a);
System.out.println(d);
} else{
System.out.println("dati da eliminare " + a);
}
});
This should work given you pass in proper data (there may be an exception raised by format.parse(d)).

selecting files with regex

I'm doing a project at college where my method is using a regex to pull the date out of the name of each of the log files in a folder, and then, after comparing the millisec stamp with one defined by the Ndays parameter passed in, delete it if its older than the required date (eg; 30 days ago- params passed in will be 30 and the file that contains the logs)....
Much appreciate any assistance.....my below attempt isn't working.....
public void deleteFilesOlderThanNdays( int Ndays, String aFolder) throws
ParseException{
File directory = new File(aFolder);
if(directory.exists()){
File[] logFiles = directory.listFiles();
for (File log: logFiles) {
String name= log.getName();
//Pattern dateFind = Pattern.compile("\\d\\{0,10}");// or
Pattern dateFind = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
Matcher dateSearch =dateFind.matcher(name);
while(dateSearch.find()){
String logDate = dateSearch.toString();
SimpleDateFormat format= new SimpleDateFormat("yyyy.mm.dd");
Date date= format.parse(logDate);
long cutOffPoint = System.currentTimeMillis() - (Ndays* 24*60*60*1000);
if(date.getTime()< cutOffPoint){
log.delete();
}
}
}
}
}
It turned out to be the simple mistakes pointed out by the replies above, the mm in the date format instead of the MM, and calling the group() method in place of toString() method on the Matcher object..regards guys....

Avoiding a particular check for YYYY-MM--dd format in date

I have below method in which different date patterns have been handled
below is the method in which different date formats have been handled now
now for the particulat format YYYY-MM-dd i don't want it to go for the check where we are prefixing 20 before in code please advise how can i skip that part lets say if the date pattern is YYYY-MM-dd then avoid the logic of prefixing 20 in front of year
below is my code
public java.util.Date extractDate(String dateStr, String dateType) {
String[] datePatternsOfUk = { "d-M-yy", "d-M-yyyy", "d/M/yy", "d/M/yyyy", "yyyy-MM-dd","dd-MM-yy", "dd-MMM-yy","dd-MMM-yyyy","dd-MM-yyyy",
"dd/MM/yy","dd/MMM/yy","dd/MMM/yyyy"};
String[] datePatternsOfUs = { "M-d-yy","MM-dd-yy","M/d/yy","MM/dd/yy", "MM/dd/yy", "MMM-dd-yy",
"MMM/dd/yy", "MMM-dd-yyyy", "MM-dd-yyyy", "MMM/dd/yyyy",
"MM/dd/yyyy" };
java.util.Date date = null;
String[] datePatterns = datePatternsOfUk;
if (dateType.equals("US")) {
datePatterns = datePatternsOfUs;
} else if (dateType.equals("UK")) {
datePatterns = datePatternsOfUk;
}
///******code should not go in this check where date pattern is YYYY-MM-dd
int p = dateStr.lastIndexOf("/");
if (p == -1) {
p = dateStr.lastIndexOf("-");
}
String firstSubstring = dateStr.substring(0, p + 1);
String secondSubstring = dateStr.substring(p + 1);
if (p != -1 && secondSubstring.length() <= 2) {
secondSubstring = Integer.toString(2000 + Integer.parseInt(secondSubstring));
dateStr = firstSubstring + secondSubstring;
}
///****************************************//
try {
date = DateUtils.parseDate(dateStr, datePatterns);
} catch (ParseException ex) {
logger.error("##$$$$$### Error in invoice inside extractDate method : ##$$$$$$#### "
+ ErrorUtility.getStackTraceForException(ex));
}
return date;
}
You could avoid trying any inappropriate pattern by checking if the string "looks like" the pattern before parsing with the pattern.
The general way to do this is:
String datePattern = "yyyy-MM-dd"; // for example
String input;
if (input.matches(datePattern.replaceAll("\\w", "\\d"))) {
// the input looks like the pattern
// in this example "dddd-dd-dd" where "d" is any digit
// so go ahead and try the parse
}
You can enhance this logic to add:
if (input.matches("\\d\\d\\D.*")) {
// then it only has a two digit year, so add "20" to the front
}
if (!dateStr.equals("YYYY-MM-dd")) {
// code
}

How to get a txt file and compare it with array for a drop down list using webdriver/java?

Here is the scenario:
1) create a file with input string Sep 2015 2) Collect the drop down list into an array 3) if array equals to string(which is in that file) come out of loop else downloads new month report and overwrites the same text file with new month name.
We tried with string in the code, but didn't created a file(don't know how to link that txt file to code to check if the array equals to string and iteration has to happen).
Here is the code:
driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
WebElement mSelectElement = driver.findElement(By.xpath("//select[#id='ReportViewerControl_ctl04_ctl03_ddValue']"));
List<WebElement> optionsList = mSelectElement.findElements(By.tagName("option"));
String oldMonth = "";
//if (optionsList.size() > 1) {
// Here considering first string from the optionsList(Here first
// string is 'All')
// String newMonth = optionsList.get(0).getText();
//}
for (int i = 2; i < optionsList.size(); i++) {
WebElement element = optionsList.get(i);
String newMonth = element.getText();
if (!oldMonth.equals("All") && !newMonth.equals("All")) {
if (newMonth.equals(oldMonth)) {
// IF the string are same, nthng we need to do
} else if (!newMonth.equals(oldMonth)) {
/*
* If the string are not same,then i.e., considered as new
* Month, download the new month details
*/
element.click();
driver.findElement(By.xpath(".//*[#id='ReportViewerControl_ctl04_ctl00']")).click();
Wait(200000);
//Click on File save button
driver.findElement(By.xpath(".//*[#id='ReportViewerControl_ctl05_ctl04_ctl00_Button']")).click();
//wait time to load the options
Wait(20000);
driver.findElement(By.xpath(".//*[#id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a")).click();
//fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
//fprofile.setPreference( "pdfjs.disabled", true );
Wait(10000);
// driver.manage().timeouts().implicitlyWait(10,
// TimeUnit.SECONDS);
System.out.println( "New month data downloaded in csv format:==>"+newMonth);
break; } }
oldMonth = newMonth;
} }
First, I recommend to use Select instead of WebElement.
Select myDropdown = new Select(driver.findElement(By.xpath("//select[#id='ReportViewerControl_ctl04_ctl03_ddValue']")));
Second, I don't see any statements that create or read file. Can you show me the code? Personally, I prefer Apache CommonsIO.
FileInputStream in = new FileInputStream("month.txt");
try {
String oldMonth = IOUtils.toString(in);
} finally {
in.close();
}

Check date format before parsing

I am parsing several documments with the field Duration. But in the differents files, it is in differnt formats, ex:
"Duration": "00:43"
"Duration": "113.046"
"Duration": "21.55 s"
I want to parse all of them to the format "Duration": "113.046", how could I check before any parsing in wich format it is??
Some conditions before this piece of code, because this is not right for all of them:
Long duration;
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
try {
Date durationD = sdf.parse(totalDuration);
Date zeroSec = sdf.parse("00:00:00");
duration = durationD.getTime() - zeroSec.getTime();
} catch (Exception e) {
duration = Long.parseLong(totalDuration);
}
Thanks in advance
You could match the pattern with help of regex and then format accordingly. Here's a kickoff example:
Map<Pattern, DateFormat> dateFormatPatterns = new HashMap<Pattern, DateFormat>();
dateFormatPatterns.put(Pattern.compile("\\d{1,2}:\\d{2}"), new SimpleDateFormat("H:m"));
dateFormatPatterns.put(Pattern.compile("\\d{1,3}\\.\\d{3}"), new SimpleDateFormat("s.S"));
dateFormatPatterns.put(Pattern.compile("\\d{1,2}\\.\\d{2} s"), new SimpleDateFormat("s.S 's'"));
String[] strings = { "00:43", "113.046", "21.55 s" };
DateFormat finalFormat = new SimpleDateFormat("HH:mm:ss");
for (String string : strings) {
for (Pattern pattern : dateFormatPatterns.keySet()) {
if (pattern.matcher(string).matches()) {
Date date = dateFormatPatterns.get(pattern).parse(string);
String formattedTime = finalFormat.format(date);
System.out.println(formattedTime);
break;
}
}
}
This yields here
00:43:00
00:01:53
00:00:21
If these are all your known input formats, then convert your input to your expected date format.
Just string-replace all : with . and remove s.
Do not forget to strip the spaces, too. By the way, "113.046" seems a bit odd date format to me - if I were in your shoes, I would have used some of the standard date time formats and convert the irregular ones.
My solution, not smart at all:
long DurationFixer(String duration){
long durationLong = 0;
if(duration.contains(":")){
DateFormat sdf = new SimpleDateFormat("mm:ss");
try {
Date durationD = sdf.parse(duration);
Date zeroSec = sdf.parse("00:00:00");
durationLong = durationD.getTime() - zeroSec.getTime();
} catch (Exception e) {
durationLong = (Long.parseLong(duration))/1000;
}
}
else{
String r = "";
if(duration.contains("s")){
for (int i = 0; i < duration.length()-2; i ++) {
if ((duration.charAt(i) == '.'))
break;
else
r += duration.charAt(i);
}
}
durationLong = Long.valueOf(r);
}
return durationLong;
}
If someone could find a better solution, please, tell me.
Thanks everybody!

Categories