Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. var EasyFtp = require('easy-ftp');
  2. var ftp = new EasyFTP();
  3. var config = {
  4. host: '',
  5. port: 21,
  6. username: '',
  7. password: ''
  8. };
  9.  
  10. ftp.connect(config);
  11.  
  12. var filesFrom=['/file1.txt','/anotherFile.txt','/moreFiles.txt','/a.txt','/x.txt']
  13. var filesTo=['/archived/file1.txt','/archived/anotherFile.txt','/archived/moreFiles.txt','/archived/a.txt','/archived/x.txt']
  14.  
  15. for (var i = 0; i < filesFrom.length; i++) {
  16. ftp.mv(filesFrom[i], filesTo[i], function(err, newPath){
  17. if (err) { console.log(err) }
  18. });
  19. };
  20.  
  21. ftp.close();
  22.  
  23. function mvFiles(ftpObj, fromArray, toArray, callback) {
  24. let index = 0;
  25. let results = [];
  26.  
  27. function next() {
  28. if (index < fromArray.length) {
  29. let i = index++;
  30. ftpObj.mv(fromArray[i], toArray[i], function(err, newPath) {
  31. if (err) {
  32. callback(err);
  33. } else {
  34. results[i] = newPath;
  35. next();
  36. }
  37. });
  38. } else {
  39. callback(null, results);
  40. }
  41. }
  42. next();
  43. }
  44.  
  45. ftp.connect(config);
  46.  
  47. var filesFrom =['/file1.txt','/anotherFile.txt','/moreFiles.txt','/a.txt','/x.txt'];
  48. var filesTo =['/archived/file1.txt','/archived/anotherFile.txt','/archived/moreFiles.txt','/archived/a.txt','/archived/x.txt'];
  49.  
  50. mvFiles(ftp, filesFrom, filesTo, function(err, newPaths) {
  51. ftp.close();
  52. if (err) {
  53. // process error here
  54. } else {
  55. // all done here
  56. }
  57. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement