One Star

[resolved] return name for most recently modified file

I want to search a directory and return the name of the most recently modified file, with a certain FileMask value.
I know that tWaitForFile can be used if I want to monitor the folder, but I don't want to monitor it. I just want the name of most recently modified file, at the time the process is run.
I also know that tFileProperties will return the modified time for each file fed to it. But, I don't know how to store that data in a temporary array (or something), within Talend, and then pull the max date from said array.
1 ACCEPTED SOLUTION

Accepted Solutions
One Star

Re: [resolved] return name for most recently modified file

Ok, I figured out how to do it
System.out.println(array);
context.MaxDate=String.valueOf(array.pollLast());
System.out.println("context.MaxDate is: "+context.MaxDate);

Starting job FindMostRecentFile at 12:01 24/06/2008.

context.MaxDate is: 1213754467354
Job FindMostRecentFile ended at 12:01 24/06/2008.

or you can use this instead, gives the same results
System.out.println(array);
context.MaxDate=Long.toString(array.pollLast());
System.out.println("context.MaxDate is: "+context.MaxDate);
9 REPLIES
Community Manager

Re: [resolved] return name for most recently modified file

Hello
Just take an example, in this case, store the last update date of all the files into a collection and pull the max date.
Please see the screenshot.
Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] return name for most recently modified file

Ok, I got what you posted to work - but what I need to do is get the file name of the file with that max date value.
(Sorry, I'm not a Java programmer, so I'm slow on the Java coding side of things)
Community Manager

Re: [resolved] return name for most recently modified file

Hello
what I need to do is get the file name of the file with that max date value.

Define a context variable: maxDate (data type: long), type in the code below on tJavaFlex
context.setProperty("maxDate", array.pollLast());

then, iterate all the files again and use this context do a inner join.
Best regards
shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] return name for most recently modified file

what I need to do is get the file name of the file with that max date value.

Define a context variable: maxDate (data type: long), type in the code below on tJavaFlex
context.setProperty("maxDate", array.pollLast());

then, iterate all the files again and use this context do a inner join.

Ok, I did that, and now I'm getting this error:
Starting job FindMostRecentFile at 11:22 23/06/2008.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method setProperty(String, String) in the type Properties is not applicable for the arguments (String, Long)
at sqlbasedproperties.findmostrecentfile_0_1.FindMostRecentFile.tFileList_1Process(FindMostRecentFile.java:1106)
at sqlbasedproperties.findmostrecentfile_0_1.FindMostRecentFile.runJobInTOS(FindMostRecentFile.java:1242)
at sqlbasedproperties.findmostrecentfile_0_1.FindMostRecentFile.main(FindMostRecentFile.java:1140)
Job FindMostRecentFile ended at 11:22 23/06/2008.

Community Manager

Re: [resolved] return name for most recently modified file

Hello
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method setProperty(String, String) in the type Properties is not applicable for the arguments (String, Long)

try
context.setProperty("maxDate",  String.valueOf(array.pollLast()));

when using this context, you need to convert string to long. for example:
long s=Long.parseLong(context.getProperty("maxDae"));

Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] return name for most recently modified file

Hello
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method setProperty(String, String) in the type Properties is not applicable for the arguments (String, Long)

try
context.setProperty("MaxDate",String.valueOf(array.pollLast()));


Ok, I put this into my End Code in tJavaFlex_1
System.out.println(array);
System.out.println("The max date is: "+array.pollLast());
context.setProperty("MaxDate",String.valueOf(array.pollLast()));
System.out.println("context.MaxDate is: "+context.MaxDate);

but the value of array.pollLast() isn't getting populated to MaxDate:
-If I have MaxDate set as a string, I get this (for the Execution output)
Starting job FindMostRecentFile at 11:47 24/06/2008.

The max date is: 1213754467354
context.MaxDate is:
Job FindMostRecentFile ended at 11:47 24/06/2008.

-If I have MaxDate set as a long, I get this (for the Execution output)
Starting job FindMostRecentFile at 11:45 24/06/2008.

The max date is: 1213754467354
context.MaxDate is: null
Job FindMostRecentFile ended at 11:45 24/06/2008.
One Star

Re: [resolved] return name for most recently modified file

I tried another string conversion command, but that doesn't populate the value to MaxDate (or MaxDate2, for testing purposes) either
System.out.println(array);
System.out.println("array.pollLast()is: "+array.pollLast());
context.setProperty("MaxDate",Long.toString(array.pollLast()));
System.out.println("Long.toString(array.pollLast()) is: "+Long.toString(array.pollLast()));
System.out.println("context.MaxDate is: "+context.MaxDate);
context.setProperty("MaxDate2",String.valueOf(array.pollLast()));
System.out.println("String.valueOf(array.pollLast()) is:"+String.valueOf(array.pollLast()));
System.out.println("context.MaxDate2 is: "+context.MaxDate2);

Starting job FindMostRecentFile at 11:57 24/06/2008.

array.pollLast()is: 1213754467354
Long.toString(array.pollLast()) is: 1210730892193
context.MaxDate is:
String.valueOf(array.pollLast()) is:1210385310021
context.MaxDate2 is:
Job FindMostRecentFile ended at 11:57 24/06/2008.

also, the values pulled by the two conversions aren't the same as what is pulled by
One Star

Re: [resolved] return name for most recently modified file

Ok, I figured out how to do it
System.out.println(array);
context.MaxDate=String.valueOf(array.pollLast());
System.out.println("context.MaxDate is: "+context.MaxDate);

Starting job FindMostRecentFile at 12:01 24/06/2008.

context.MaxDate is: 1213754467354
Job FindMostRecentFile ended at 12:01 24/06/2008.

or you can use this instead, gives the same results
System.out.println(array);
context.MaxDate=Long.toString(array.pollLast());
System.out.println("context.MaxDate is: "+context.MaxDate);
One Star

Re: [resolved] return name for most recently modified file

Ok, I got it to work. Here are the screenshots (for anyone else who might need this information)

Thanks shong!