cancel
Showing results for 
Search instead for 
Did you mean: 

Issue in picking a most recent file from ftp server

giridhar_vegi
Participant
0 Kudos

Hi Experts,

                    i had a requirement where i need to pick most recent file based on date and time in which we are getting multiple files for every hour.for Example :if a file is placed at11:00AM on 03/06/2015, with file name Activity.20150306.0010 our PI job will run at 11:05AM and it should pick file named Acitivity.20150306.1100.0010.txt. and i need to pick the most recent file and it has to be processed by the pi.please help me how to achieve this.

Thanks

Giridhar

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

HI Giridhar,

It is possible to implement. but you need to do few thing before PI start processing your message.

The High level view will be like this below.

---------------------------------

Step1: you need to create 2 folders in your source FTP.

Step 2: Need to install java s/w in FTP server and need to set classpath.

Step 3: Write a Java Program which picks recent file from the 1st folder.(place all files in this folder instead of source directory mentioned in CC) and in the same program write code to move that file from 1st folder to 2nd folder where your CC will picks that file.

Compile the java code and will create a .class file

Step 4: Create a .bat file in which write a single command which runs compiled java class.

Step 5: Use this .bat file in your File Sender CC processing parameters,                                       "Run OS Command Before Message Processing"

Try this approach if you struck at any step, then I will try to post the required code (Java/.bat Code).

Regards,

Nagesh

iaki_vila
Active Contributor
0 Kudos

Hi NageswaraRao Donepudi,

Your approximation is very helpful and you can help a lot of SCN members. I encourage you to write a blog with your solution.

regards.

giridhar_vegi
Participant
0 Kudos

Hi Nageswararao ,

                         could you please post the code.

Thanks

Giridhar

praveen_sutra
Active Contributor
0 Kudos

Hi Giridhar,

Just on how to pick file from FTP location using java mapping.

you can definitely make some changes to the code to pick the latest file

Hope this helps.

thanks and regards,

Praveen T

Former Member
0 Kudos

Hi Giri,

I just written this code and tested with 2 folders and set of files. working fine.

Java Code.

--------------------------------------------------------------------------------------------------------------------

import java.io.File;

/** @author nageswara */

public class RecentFilePick {

  

    public static void main(String argv[]){

  

     File dir1 = new File("D:\\TempSourceDir");

     if(dir1.isDirectory()){

          if(dir1.list().length == 0){ }

          else{

                File[] files = dir1.listFiles();

                File RecentFile = files[0];

                for (int i = 1; i < files.length; i++) {

                     if (RecentFile.lastModified() < files[i].lastModified()) {

                         RecentFile = files[i];

                       }

                    }

                    RecentFile.getAbsoluteFile().renameTo(new                      File("D:\\SourceDir\\"+RecentFile.getName()));

                                   

          }

        }

    }

}

--------------------------------------------------------------------------------------------------------------------------

need to create 2 folders namely TempSourceDir & SourceDir.

after compiling.

Batch file command below

-------------------------------------------

java -cp d:\RecentFilePick

-----------------------------------------

Note: I have tested above code in my PC, it is working fine, if any changes required related to paths extra you can make accordingly.

Regards,

Nagesh

Former Member
0 Kudos

Hi Vila,

Thanks for your suggestion, I will write blog once I get free time.

Regards,

Nagesh

vkaushik82
Active Participant
0 Kudos

This is not possible in one scenario as suggested above.

But you can put a batch script to copy most recent file to a directory from where channel can pick, run this script before message processing in adapter.

This is one of the solution I can think.

iaki_vila
Active Contributor
0 Kudos

Hi Vinkrant,

I thought in that possibility to suggest to Girdhar with this link shell - How do I change folder timestamps recursively to the newest file? - Unix &amp; Linux Stack E..., but i'm thinking if you do the bash file in each poll you are changing the oldest to the newest in the first poll, but in the second poll you change again from the newest to the oldest.

Regards.

giridhar_vegi
Participant
0 Kudos

could you please post the script code

vkaushik82
Active Participant
0 Kudos

check this out you might have to modify commands as per need but this will give the idea.

shell script - Get the newest file from directory structure year/month/date/time - Unix &amp; Linux ...

giridhar_vegi
Participant
0 Kudos

hi ,

          Could you please post the script code.

Thanks

Giridhar

iaki_vila
Active Contributor
0 Kudos

Hi Girdhar,

AFAIK it is not possible in one scenario because the FTP adapter has not processing sequence and you always take the file by name order.

Regards.