We help IT Professionals succeed at work.
Get Started

FTP search to a directory using an array of search patterns

kalyangkm asked
Last Modified: 2016-06-19
Hi Folks,

I have a rek to search a FTP directory for a list of filenames against an array of filepatterns. If there is match to the filepattern in the directory with a filename, that filename should be returned if otherwise it the program should return a blank or " ".

if the input is a array of filepatterns as below

and if the FTP directory contains

then the output should be an array of Strings as follows
" "
" "

So far I have the following program working for extracting the matched filenames in the directory based on filepatters, but it suppress the output if there is not match, instead I would need to return " " when there is no match for a particular pattern.

First I have created the following class

public class FTPMultipleFileLookup implements FTPFileFilter {

        FTPClient ftpClient = new FTPClient();
        FTPFile[] result;
        List<String> filePatterns;
        String pathname;
        FTPFileFilter filter;
        public FTPMultipleFileLookup(List<String> filePatterns) {
            this.filePatterns = filePatterns;


	 public boolean accept(FTPFile ftpFile) {

	return (ftpFile.isFile() && filePatterns.contains(ftpFile.getName()));


Open in new window

Then I used the above code in the Main program in which I perform the actual search but like I said the search only extracts the file pattern match records and suppress anyvalue that doesnt match, instead I need to return " ". Please suggest modification to the following program or any other approach that works.

public class MultipleFilelookup {
    public static void main(String[] args) {
    	 String server = "XXXXX";
         int port = XX;
         String user = "XXX";
         String pass = "XXXXXX";

         //final String file = "lookup.txt";
         //List<String> filePatterns = new ArrayList<String>();
         List<String> filePatterns = Arrays.asList("lookup.txt","loookup.txt");
         FTPClient ftpClient = new FTPClient();
             ftpClient.connect(server, port);
             ftpClient.login(user, pass);
             String dirToSearch = "XXXXXXX";
             FTPMultipleFileLookup filter = new FTPMultipleFileLookup(filePatterns) {
                 public boolean accept(FTPFile ftpFile) {
                   boolean accept = false;
                	String fileName = ftpFile.getName();
                	for(String pattern : filePatterns){
                			accept = true;
                	return accept;

            FTPFile[] result = ftpClient.listFiles(dirToSearch, filter);
            if (result != null && result.length > 0) {
                System.out.println("SEARCH RESULT:");
                //InputStreamReader reader = new InputStreamReader(result);
                for (FTPFile aFile : result) {
        } catch (IOException ex) {
        } finally {
            if (ftpClient.isConnected()) {
                try {
                } catch (IOException ex) {

Open in new window

Watch Question
Top Expert 2015
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE