Link to home
Start Free TrialLog in
Avatar of kalyangkm
kalyangkmFlag for United States of America

asked on

FTP search to a directory using an array of search patterns

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 " ".

Example
if the input is a array of filepatterns as below
loook
lookup
loookup

and if the FTP directory contains
lookup.txt

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

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;
             }
   

       
          

	 @Override
	 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();
  
      try{
             ftpClient.connect(server, port);
  
             ftpClient.login(user, pass);
  
             ftpClient.enterLocalPassiveMode();
  
             String dirToSearch = "XXXXXXX";
 
             FTPMultipleFileLookup filter = new FTPMultipleFileLookup(filePatterns) {
 
                @Override
                 public boolean accept(FTPFile ftpFile) {
 
               
                   boolean accept = false;
                	String fileName = ftpFile.getName();
                	for(String pattern : filePatterns){
                		if(fileName.contains(pattern)){
                			accept = true;
                			break;
                		} 	
                	}
                	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) {
                    System.out.println(aFile.getName());
                }
            }
 
            ftpClient.logout();
 
            ftpClient.disconnect();
 
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

Open in new window

Avatar of gheist
gheist
Flag of Belgium image

You need to download and scan each file. There is no ftp command like grep.
Avatar of kalyangkm

ASKER

So, you mean to say I can't do multiple file search using an array list/vector to achieve this? So is there no other option than the class FTPfile which implements the method accept which is only a Boolean return for all the files that exists?
ASKER CERTIFIED SOLUTION
Avatar of gheist
gheist
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This looks like it could work. I am not a pro in Java. Can you please provide a sample code for the Parser based on my rek?
regex is called pattern in java.
gheist,

I would need a sample code based on my rek, I see the following sample code. But not sure how to fit it for my rek.

http://www.massapi.com/class/ft/FTPFileEntryParser.html
Hi gheist,

Could you please respond on how to implement the FTPFileEntryParser to parse the files in the directory?
Hi Folks,

Is there anyone who has worked on the FTPFileEntryParser or if not can anyone atleast have a look into how it can be used to parse the filenames in a directory. Any inputs would be appreciated.