SFTP Node.js - Is It Possible To List Files Using Wildcards?
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
Answer
Mmm, I don't think this is possible in ssh2, but what you can do is list them algorithmically and access each one, pseudo-code:
Connect SFTP
List Folders -> Save this to a dictionary
For each folder in Folders
List Folders - > Save this to a dictionary
At the end of it you'll have a dictionary object with the full path of the remote server, like so
{
sftp: {
"subfolders": {
"0": {
"name": "/rootfolder",
"subfolders": {
"0": {
"name": "/rootfolder",
"subfolders": {
...
}
}
}
}
}
}
}
From that you can easily access whatever you need by doing
sftp["/rootfolder"]["/subfolder1"]... etc
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error