0

I have two files on my SFTP server

xyz07012014abc.txt
xyz06072014abc.txt

I have a Java program having matchpattern as follows: month="07" and year="2014"

matchPattern = "*" + month + "*" + year + "*";

Above matchpattern gets me both the files. But, I want only following file:

xyz07012014abc.txt

How can I modify my matchpattern to get correct result.

EDIT

I am using following java method and feeding above matchpattern to it.

private void getFilesFromFTP(String sftpBase, String matchPattern) throws JSchException, SftpException{
    log.debug("Downloading reward files from FTP server...");
    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;
    JSch jsch = new JSch();
    session = jsch.getSession(REWARDS_SFTP_USER, REWARDS_SFTP_SERVER, 22);
    session.setPassword(REWARDS_SFTP_PASSWORD);
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();

    channel = session.openChannel("sftp");
    channel.connect();
    channelSftp = (ChannelSftp) channel;
    channelSftp.cd(sftpBase);

    Vector<ChannelSftp.LsEntry> list = channelSftp.ls(matchPattern);
    for (ChannelSftp.LsEntry entry : list) {
        channelSftp.get(entry.getFilename(), TEMP_DIRECTORY_PATH + entry.getFilename());
    }

    channel.disconnect();
    session.disconnect();
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
  • Your `matchPattern` is not correct regex (first `*` has nothing before it). Provide some correct example which will explain your problem and let us reproduce it. – Pshemo Jul 11 '14 at 23:02
  • That doesn't look like a regex. In particular, in a regular expression `*` matches 0 or more of the previous character/group. Your pattern as you have written it (`"*07*2014*`) shouldn't match either filename. – murgatroid99 Jul 11 '14 at 23:02
  • It does match both the filenames. – hrishikeshp19 Jul 11 '14 at 23:11
  • Then this is not regex you are asking about. It seems that `*` is wildcard, not quantifier. – Pshemo Jul 11 '14 at 23:16
  • Pshemo: Okay. I was not aware of that. Please feel free to edit tags per your wish. – hrishikeshp19 Jul 11 '14 at 23:18
  • I would consider changing it to something more related to OS console command. I am thinking about `bash` or `shell` but honestly I am not the right person to decide which (if any of them) is more appropriate here. For now I only removed regex tag. – Pshemo Jul 11 '14 at 23:23
  • Is the post is not related to java + regex? – Braj Jul 11 '14 at 23:25
  • The `ls` method of the `channelSftp` object does not accept a regex as parameter but only a string. – Casimir et Hippolyte Jul 11 '14 at 23:25
  • thanks everyone, I solved it. I used "??" instead of "*". – hrishikeshp19 Jul 11 '14 at 23:34

1 Answers1

0

Following is link to Jsch JavaDoc:

http://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/ChannelSftp.html#ls(java.lang.String)

It says:

ls

public Vector ls(String path)
          throws SftpException
lists the contents of a remote directory.
Parameters:
path - a pattern relative to the current remote directory. The pattern can contain glob pattern wildcards (* or ?) in the last component (i.e. after the last /).
Returns:
a vector of ChannelSftp.LsEntry objects.
Throws:
SftpException

It asks you to use glob pattern wildcards. So, I changed my wildcard from

matchPattern = "*" + month + "*" + year + "*";

to

matchPattern = "*" + month + "??" + year + "*";

and the problem is fixed.

Thanks everyone for help.

hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141