Kush Sahu
Kush Sahu

Reputation: 399

How to get only directories and text files using SFTP channel of JSch?

Hi I am developing an application which connects to remote server and browses through different directories.

Here I want to show only directories and text files to user. Using SFTP channel in JSch, I can execute ls method. But this method can give me result for either in this format "*" or "*.txt". Using ls separately I can get directories list and text file list. Since I am using it separately I have to use 2 different ls methods like:

sftpChannel.ls("*"); 
sftpChannel.ls("*.txt");

1st gives me all entries from which I have to loop and filter directories. In second, I get all text files.

How can I get directories list and text file list using minimum code. I don't want to loop twice. Thanks

Upvotes: 1

Views: 4585

Answers (2)

Ganesh Giri
Ganesh Giri

Reputation: 1161

We can use like this, read directories and files also.   

 public List<String> readRemoteDirectory(String location){
            System.out.println("Reading location : "+location);
            Session session = null;
            Channel channel = null;
            ChannelSftp channelSftp = null;
            
            List<String> filesList = new ArrayList<String>();
            
            String separator = getSeparator();
            
            try{
                JSch jsch = new JSch();
                session = jsch.getSession(remote_server_user,remote_server_ip,22);
                session.setPassword(remote_server_password);           
                java.util.Properties config = new java.util.Properties();
               
                config.put("PreferredAuthentications", "publickey,keyboard-interactive,password");
                session.setConfig(config);
                session.connect();
                channel = session.openChannel("sftp");
                channel.connect();
                channelSftp = (ChannelSftp)channel;
    
                channelSftp.cd(location);
               
                Vector filelist = channelSftp.ls("*");
                for(int i=0; i<filelist.size();i++){
                    LsEntry entry = (LsEntry) filelist.get(i);  
                    if (".".equals(entry.getFilename()) || "..".equals(entry.getFilename())) {
                        continue;
                    }
                    
                    if(entry.getAttrs().isDir()){
                        System.out.println(entry.getFilename());
                        //System.out.println("Entry"+location+separator+entry.getAttrs());
                        filesList.add(entry.getFilename());
                    }
                    
                }
               
               
    
            }catch(Exception ex){
                ex.printStackTrace();
                logger.debug(ex.getMessage());
               if(ex.getMessage().equals("No such file")){
                   logger.debug("No Such File IF");
               }
                
                
            }finally{
                 channel.disconnect();
                 session.disconnect(); 
            }
            
            return filesList;
        }

Upvotes: 2

Martin Prikryl
Martin Prikryl

Reputation: 202222

Use ls(""). Then loop the returned entries, and select only those you want.

I.e. those that have LsEntry.getFilename() ending with ".txt" or LsEntry.getAttrs().isDir().

Upvotes: 4

Related Questions