Use Peoplecode to delete files from remote FTP site

399 Views Asked by At

I am trying to use a PeopleCode app engine to delete files from a remote server. GetAttachment would work except that I want to loop through a the whole directory on the server and match files like march.csv.

Also, I am needing to do this by date range. *.csv between date 1 and date 2.

Get attachment seems to require that you know the file name and individually delete the files. So pattern pattern matching will not work.

I can get the list and check dates using java objects:

Local JavaObject &joFile = CreateJavaObject("java.io.File", SFO_DEL_FTP_AET.FTPDIRECTORY); Local JavaObject &joFileList; Local JavaObject &reflectArray = GetJavaClass("java.lang.reflect.Array"); Local JavaObject &dttm_format = CreateJavaObject("java.text.SimpleDateFormat", "yyyy-MM-dd");

but I can only use that with local servers.

Is there a way to actually connect to the server, then use the java objects?

Thanks, JPS

1

There are 1 best solutions below

0
On

You could use Apache Commons NET library for this. One of the packages is intended for FTP/FTPS access.

FTPClient ftpClient = new FTPClient();
ftpClient.connect(server, port);
ftpClient.login(user, pass);
 
FTPFile[] files = ftpClient.listFiles("/yourDir");     

Once you get the file list, you could retrieve the name from the FTPFile object.

In order to delete a file, you could use FTPClient's deleteFile method.

public boolean deleteFile(String pathname)

Deletes a file on the FTP server. Parameters: pathname - The pathname of the file to be deleted. Returns: True if successfully completed, false if not.

 
for (FTPFile file : files) 
{
    String fileName = file.getName();
    /*if (file.isDirectory()) 
       handle directory access*/
    
    //use getAttachment from the fileName
  
    //in order to delete a file, call FTPClient's deleteFile Method  
    //ftpClient.deleteFile(filePath);
}

ftpClient.logout();
ftpClient.disconnect();

You can download the NET library here