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。這是最好的解決方案。 –