sshtools / maverick-synergy

Next Generation Java SSH API
https://jadaptive.com
GNU Lesser General Public License v3.0
96 stars 26 forks source link

Having a wild card * in the file path to find files. #40

Closed jamoros closed 2 years ago

jamoros commented 2 years ago

Greetings again!

I am currently running into an issue where I'd like to get information about a file or set of files where the file path has a wildcard. For example I tried the following:

SftpFileAttributes sftpFileAttribute = stat("/home/user/MyFile_2022_06_01*.csv");

In the directory I have a file

/home/user/MyFile_2022_06_01_Daily.csv

I expect that file to be returned in the attributes but I am getting no file found.

Is there an alternative way to do this or am I doing something wrong with the above? Im aware that stat returns attributes of a file or a directory, but I'd like to get something working using the wildcard.

As always, any information is greatly appreciated!

EDIT

I wrote a work around, but I wonder if there's a more efficient way to do this:

if (fileName.contains("*")) {
    List < SftpFile > filesToDownload = new ArrayList < > ();
    Pattern filePattern = Pattern.compile(StringUtils.replace(fileName, "*", ".*"));
    SftpFile[] filesFound = ls(folderPath);
    for (SftpFile sftpFile: filesFound) {
        System.out.println("File found: " + sftpFile.getAbsolutePath());
        Matcher matcher = filePattern.matcher(sftpFile.getFilename());
        if (matcher.matches()) {
            filesToDownload.add(sftpFile);
        }
    }
    System.out.println("Files found: " + filesToDownload.size());
}
ludup commented 2 years ago

It does not work like that.

SFTP does not support wildcards. The stat call requires the path to a real file, if no file exists it will throw a SftpStatusException.

You can do a recursive get via SCP but that actually downloads the file, not just lists them. I'm not sure how OpenSSH's recent announcement about moving SCP to SFTP affects this.

jamoros commented 2 years ago

Ok, thank you! I added the code for the little work around and its worked so far except for yet another issue I'm dealing with.

Cheers!