How to list files between two dates

Hi all,

I want to retrieve a list of files that are in specific date range. I used services like pub.client.ftp:login, then by pub.client.ftp:cd…
how do i proceed after this.
My input will be dirName, dat1 and date2
and output will be fileNameList. I have written a Java service which will get the list of files that retrieve the files between date range. After pub.client.ftp:cd am branching on returnCode and if its 250 then am invoking this Java service that will take dirName, date1 and date2 as inputs(and mapping exactly) and when i execute this service that java service is failing and throwing Null Pointer exception.

Please suggest me where i went wrong and with some better alternatives.

Regards,
Datta

How can we tell you why your code is failing without seeing it? Please post your code here and maybe we can give you a clue as to why you’re getting a NullPointerException.

Perhaps a more important question is: why is there a requirement for getting files within a date range? What are you trying to achieve ultimately?

  • Percio

Hi Percio,

The following is my Java code that lists files between date range. We are developoing this as part of utility service which searches in a list of directories(read from a config XML file) and between the given date range.

IDataCursor pipelineCursor = pipeline.getCursor();
String fileDir = IDataUtil.getString( pipelineCursor, “fileDir” );
String date1 = IDataUtil.getString( pipelineCursor, “date1” );
String date2 = IDataUtil.getString( pipelineCursor, “date2” );
String errorMessage = “”;
String fileListErrorMessage = “”;

   File[] dirList = new File(fileDir).listFiles();

DateFormat df1 = new SimpleDateFormat(“E MMM dd HH:mm:ss z yyyy”);
DateFormat df2 = new SimpleDateFormat(“E MMM dd HH:mm:ss z yyyy”);
List filteredFiles = new ArrayList();
final SimpleDateFormat FORMAT = new SimpleDateFormat(“E MMM dd HH:mm:ss z yyyy”);
long d1 =0;
long d2 =0;
try{
Date dt1 = df1.parse(date1);
Date dt2 = df2.parse(date2);
d1 = dt1.getTime();
d2 = dt2.getTime();
for ( int i = 0; i < dirList.length; i++)
{
if((dirList[i].lastModified() > d1) && (dirList[i].lastModified() < d2))
filteredFiles.add(dirList[i].getPath().toString());
}
}catch(Exception e1) {
errorMessage = e1.getLocalizedMessage();
System.out.println("Problem occured while converting the dates "+errorMessage);
IDataUtil.put( pipelineCursor, “errorMessage”, errorMessage );
}

pipelineCursor.destroy();
IDataCursor pipelineCursor_1 = pipeline.getCursor();
try{
int s = filteredFiles.size();
IData fileList = new IData[s];
for(int j=0; j <fileList.length; j++){
fileList[j] = IDataFactory.create();
IDataCursor fileListCursor = fileList[j].getCursor();
//File f = (File)fileList[j];
String f = String.valueOf(filteredFiles.get(j));
IDataUtil.put( fileListCursor, “serialNumber”, (j+1)+"");
IDataUtil.put( fileListCursor, “fileName”, f);
fileListCursor.destroy();
}
IDataUtil.put( pipelineCursor_1, “fileList”, fileList );
}catch(Exception e2){
fileListErrorMessage = e2.getMessage();
System.out.println("Problem occured while Listing Files "+fileListErrorMessage);
IDataUtil.put( pipelineCursor_1, “fileListErrorMessage”, fileListErrorMessage );
}
pipelineCursor_1.destroy();

The code is working fine if am giving the local directory say some c:\abc and input will be two dates. I am able to login using pub.client.ftp:login into that host and it sontains some files . I am invoking this java service in a flow service which reads directories and logs into that target host and supplies dirname, date1 and date2 inputs to my Java service. I am getting nullPointerException after invoking the Java service.

Thanks a lot in adv,
Regards,
Datta

I’m not sure I understand your problem exactly, but are you trying to use this code to list files in a remote FTP server directory? If so, then your code will not work. The File object is used to access local files (or files that can be accessed via a UNC path, for example.)

  • Percio

Just an idea… You may need to place your logic on the remote server and have a remote call after ftp login. Hunt the ftp command(s) for remote execution calls.

Hi Percio,

Thanks for clarifying , I know this code will not work if i execute in between ftp session that am using in my flow service. But definitely there will be some turn around solution. like executing a ftp:ls then ftp:quote command to get file time stamps and then comparing with my input dates…some thing like that. Iam trying with that currently.

I don’t know how much control you have over the file names, but you could add a timestamp to the names (ex. yyyyMMdd_MyFile.txt), which would help you in case the FTP server you’re dealing with does not support the commands you’re looking for.

  • Percio