How to get the latest file from the directory ?

 

We have the below sample files in our directory and every file has a date in the name of the file, based on that we will decide which file is the most recent rather than the file created date/ modified date.

As you can see, we have five list of files.

1. Employee_12-05-2023

2. Employee_13-05-2023

3. Employee_14-05-2023

4. Employee_15-05-2023

5. Employee_16-05-2023

 

To get the latest file from the directory we will use the below steps.

Step 1: Add file list and tJava component and configure it to get all files from the directory.

Step 2: Add the tFileProperties component and connect with tJava using the RunIf link, then provide the file path and name from the global variable and If condition.

which looks like this

 ((String)global map.get(“tFileList_2_CURRENT_FILEPATH”))

((String)globalMap.get("tFileList_2_CURRENT_FILE")).contains("Employee_"+TalendDate.getDate("DD-MM-YYYY")).

Step 3: Add tMap after tFileProperties and connect with the main link and do the fowling setting in it.

·         Create output name as “FileName”.

·         Add source column to this output.

  

Step 4: Add tLogRow to check the result. you will see the output below

  


 




Step 5: Your job design should look like in the below Image.