0

I want to have files with names File1.txt filE2.txt FIle3.txt

I want to list down all three files when I pass the fileMask as a file. basically, I want to do a case insensitive search using the channel.ls(path + fileMask , selector)

Moiz
  • 1
  • 2
  • 2
    See also https://stackoverflow.com/questions/28020060/jsch-channelsftp-ls-pass-match-patterns-in-java - it seems that the consensus is to just get all file names, and perform any filtering on your side – Hulk Oct 07 '20 at 06:38

1 Answers1

0

To search for a particular file in the sftp location , just do something like :

    remoteFileName = "fileName_[0-9]{08}.txt"
    try {
        ChannelSftp channelSftp = setupJsch();
        channelSftp.connect();
        Vector ls = channelSftp.ls(remoteFilePath);
        Pattern pattern = Pattern.compile(remoteFileName,  Pattern.CASE_INSENSITIVE);
        for (Object entry : ls) {
            ChannelSftp.LsEntry e = (ChannelSftp.LsEntry) entry;
            Matcher m = pattern.matcher(e.getFilename());
            if (m.matches()) {
                //Do something
                channelSftp.get(remoteFilePath + e.getFilename(), getLocalFileDir());
                channelSftp.exit(); //If you want to search for multiple files then do not terminate the connection here
            }
        }
    } catch (JSchException jSchException) {
        LOGGER.info("File download failed :", jSchException);
    } catch (SftpException sftpException) {
        LOGGER.info("File download failed :", sftpException);
    }
Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
  • Matcher m = pattern.matcher(e.getFilename().toUpperCase()); ??? but i want to search case insensitive, not just upper case files – Moiz Oct 07 '20 at 11:37
  • @Moiz I have modified the answer, you could set the param as `Pattern.CASE_INSENSITIVE` to get case insensitive matches, Or just convert everything to upperccase and match like i did before. – Ananthapadmanabhan Oct 07 '20 at 11:41
  • I will not have full file name, your code block works ok when remoteFileName is given in full, but i won't have full name, its a search from UI and i will receive just any part of the name. I have to search with * file_name* – Moiz Oct 07 '20 at 12:23
  • @Moiz construct a regex in code based on the input from ui and set it to `remoteFileName`. – Ananthapadmanabhan Oct 07 '20 at 12:30