Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hello everyone, I'm trying to compare filename with date inside to whether or not delete them.
Since I have 2 types of filesname : "AA_MAGAS_yyyyMMdd_hhmm" and "AA_BBB_CCC_yyyyMMdd_hhmm" , my idea was to substring the filename (from 9 to 22 for first file type and 11 to 24 for the second one), get the date out & compare it. Then use this result in a run if condition to delete it.
My job was working for only 1 file type, but since I tried a IF condition to deal with the 2 filesname, its not working anymore. Can someone have a check ?
{ context.strdate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(9,22); context.date = TalendDate.parseDate("yyyyMMdd_hhmm",context.strdate); //difference of days between current date & file date, >15 = delete (runif) context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd"); } else { context.ecodate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(11,24); context.date = TalendDate.parseDate("yyyyMMdd_hhmm", context.ecodate); context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd"); };
My error, seems like the IF condition is not working because the substring get it wrong :
java.lang.RuntimeException: java.text.ParseException: Unparseable date: "180210_0345.x"
Thanks !
This is caused by your wildcard. You cannot use a wildcard in Equals.
Try this....
if ( ((String)globalMap.get("tFileList_1_CURRENT_FILE")).indexOf("AA_MAGAS_")>-1 ) { context.strdate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(9,22); context.date = TalendDate.parseDate("yyyyMMdd_hhmm",context.strdate); //difference of days between current date & file date, >15 = delete (runif) context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd"); } else { context.ecodate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(11,24); context.date = TalendDate.parseDate("yyyyMMdd_hhmm", context.ecodate); context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd"); };
You may also need to account for NULL if the filename can be empty. I've not done that, but you should think about it.
This is caused by your wildcard. You cannot use a wildcard in Equals.
Try this....
if ( ((String)globalMap.get("tFileList_1_CURRENT_FILE")).indexOf("AA_MAGAS_")>-1 ) { context.strdate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(9,22); context.date = TalendDate.parseDate("yyyyMMdd_hhmm",context.strdate); //difference of days between current date & file date, >15 = delete (runif) context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd"); } else { context.ecodate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(11,24); context.date = TalendDate.parseDate("yyyyMMdd_hhmm", context.ecodate); context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd"); };
You may also need to account for NULL if the filename can be empty. I've not done that, but you should think about it.
Hi,
Also, as you're parsing a date from part of a filename, you should consider using the TalendDate.isDate() function, and only attempt the parseDate if it is a date.
Thanks
David