1
有什么方法可以重命名SFTP上的文件?如何使用NodeJs重命名SFTP上的文件
这是我使用的下载,我使用scp2
节点模块:
client.scp({
'host': this.host,
'username': this.username,
'password': this.password,
'path': path
},'./', function(err) {
if (err){
return callback("File: "+path+" "+err);
}else{
return callback(null);
}
});
也许我可以下载文件,与另一名上传并从SFTP旧的。但这并不完整。
我已经看过其他模块,如:https://www.npmjs.com/package/sftpjs,有一些重命名功能实现,但我不能使它的工作。任何建议?这是非常明显的用例,我找不到任何东西。
编辑: 维杰的回答是正确的。我只是做错了。
我的代码:
// Download swush file from SFTP
var Client = require('sftpjs');
var c = Client();
c.on('ready', function() {
c.list(function (err, list) {
if (err) throw err;
console.dir(list);
c.end();
});
}).connect({
'host': this.host,
'username': this.username,
'password': this.password,
}).rename(path.concat(oldName), path.concat(newName), function(err){
if (err){
console.log("Error when renaming file: "+err)
}else{
console.log("Renaming file successful.");
}
});
工作代码:
// Download swush file from SFTP
var Client = require('sftpjs');
var c = Client();
c.on('ready', function() {
c.rename(path.concat(oldName), path.concat(newName), function(err) {//only one parameter err is available for rename method.
if (err){
console.log("Error when renaming file: "+err)
}else{
console.log("Renaming file successful.");
}
});
}).connect({
'host': this.host,
'username': this.username,
'password': this.password,
});
令人惊叹!谢谢。请检查编辑,为了它的乐趣;) –
好的,谢谢。你介意告诉我为什么它没有新的关键字工作? –
请阅读http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript。这是最好的解决方案。 –