Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[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.
Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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);

View solution in original post

9 Replies
Anonymous
Not applicable
Author

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
0683p000009MCKl.png 0683p000009M7dB.png 0683p000009MBkZ.png 0683p000009MCTW.png 0683p000009MCW0.png 0683p000009MCW5.png
Anonymous
Not applicable
Author

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)
Anonymous
Not applicable
Author

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
0683p000009MCPo.png
Anonymous
Not applicable
Author

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.

0683p000009MCWF.jpg 0683p000009MCWK.jpg 0683p000009MCLr.jpg
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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
Anonymous
Not applicable
Author

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);
Anonymous
Not applicable
Author

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

Thanks shong!
0683p000009MCWG.jpg 0683p000009MCWP.jpg 0683p000009MCNC.jpg 0683p000009MCHk.jpg 0683p000009MCWU.jpg 0683p000009MCWZ.jpg 0683p000009MCWe.jpg 0683p000009MCIG.jpg