0

I'm trying to list all files in my SFTP server from a top level folder in Node.js using the npm module ssh2-sftp-client. However, I cannot find any documentation or previous posts which discuss whether using a wildcards in the file paths is possible. The file paths look like so:

../mnt/volume_lon1_01/currency/curve/date/filename.csv

There can be many different currencies, curves and dates - Hundreds in fact - I need a means of just listing every file name at the final level of the file structure.

I thought a sensible approach would be to use wildcards:

../mnt/volume_lon1_01/ * / * / * / *.csv

But this doesn't seem to work and I can't find anything to suggest it could. Can anyone advise how would be best to list every file from SFTP in Node.js?

Many thanks,

George

4

1 回答 1

1

嗯,我认为这在 ssh2 中是不可能的,但你可以做的是通过算法列出它们并访问每一个伪代码:

Connect SFTP
    List Folders -> Save this to a dictionary
    For each folder in Folders
        List Folders - > Save this to a dictionary

最后,您将拥有一个包含远程服务器完整路径的字典对象,如下所示

{
    sftp: {
        "subfolders": {
            "0": {
                 "name": "/rootfolder",
                 "subfolders": {
                     "0": {
                         "name": "/rootfolder",
                         "subfolders": {
                            ...
                         }
                      }
                  }
             }
        }
    }
}

从中您可以轻松访问所需的任何内容

sftp["/rootfolder"]["/subfolder1"]... etc
于 2018-11-23T15:50:44.000 回答