示例代码:

var Ftp = require('ftp');
var fs = require('fs');
var path = require('path'); // 首先判断参数中是否包含{dateString}
var args = process.argv.splice(2);
if (args.length < 1) {
console.error('usage: node ftpFilesGetter.js [dateString]');
process.exit(1);
}
var dateString = args[0]; // 根据dateString获得nextDay
var year = parseInt(dateString.substring(0,4)),
month = parseInt(dateString.substring(4,6)),
day = parseInt(dateString.substring(6,8));
var lastday;
if (month == 2) {
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) lastday = 29;
else lastday = 28;
} else if ([1, 3, 5, 7, 8, 10, 12].indexOf(month) != -1) lastday = 31;
else lastday = 30;
if (day < lastday) day ++;
else {
month ++;
if (month > 12) {
month = 1;
year ++;
}
}
if (month < 10) month = '0' + month;
if (day < 10) day = '0' + day;
var nextDay = `${year}${month}${day}`; // 判断下载文件所在的文件夹是否存在,如果不存在则创建
var rootDir = 'D:/test'; if (fs.existsSync(rootDir) == false) fs.mkdirSync(rootDir);
if (fs.existsSync(`${rootDir}/report_log`) == false) fs.mkdirSync(`${rootDir}/report_log`);
if (fs.existsSync(`${rootDir}/report`) == false) fs.mkdirSync(`${rootDir}/report`);
var logDir = `${rootDir}/report_log/${dateString}`; if (fs.existsSync(logDir) == false) fs.mkdirSync(logDir);
var nextDayLogDir = `${rootDir}/report_log/${nextDay}`; if (fs.existsSync(nextDayLogDir) == false) fs.mkdirSync(nextDayLogDir);
var dataDir = `${rootDir}/report/${dateString}`; if (fs.existsSync(dataDir) == false) fs.mkdirSync(dataDir); // 登陆ftp
var connectionProperties = {
host: 'xxx.xxx.xxx.xxx',
user: 'stp_username',
password: 'ftp_password'
};
var ftp = new Ftp();
ftp.on('ready', ()=>{
console.log('connect to ftp ok!');
var needFileList = [];
var callback = function() {
console.log(`2 log files + many data files, total : ${needFileList.length} files!`);
var cnt = 0;
needFileList.forEach(file => {
var filename = file.substring(file.lastIndexOf('/') + 1);
var localFile;
if (file.substring(0,12) == `log/${dateString}`) { // 今天的log
localFile = `${logDir}/${filename}`;
}
else if (file.substring(0,12) == `log/${nextDay}`) { // 明天的log
localFile = `${nextDayLogDir}/${filename}`;
} else { // data
localFile = `${dataDir}/${filename}`;
}
ftp.get(file, (err, stream)=>{
stream.once('close', ()=>{
cnt ++;
console.log(`(${cnt}) download ${file} --> ${localFile} succeed!`);
if (cnt >= needFileList.length) {
console.log('process successfully end!');
process.exit(0);
}
});
stream.pipe(fs.createWriteStream(localFile));
}); });
}
ftp.list(`log/${dateString}`, (err, list)=>{
list.forEach(item => needFileList.push(`log/${dateString}/${item.name}`));
ftp.list(`log/${nextDay}`, (err, list)=>{
list.forEach(item => needFileList.push(`log/${nextDay}/${item.name}`));
ftp.list(`sdata/S-999000`, (err, list)=>{
var sysIdList = list.map(item => { return item.name });
var cnt1 = 0; // cnt1用于记录sysId的个数,当等于sysIdList.length的时候说明遍历完了
var tmpDirList = [];
sysIdList.forEach(sysId => {
ftp.list(`sdata/S-999000/${sysId}`, (err, list)=>{
cnt1 ++;
list.forEach(item => tmpDirList.push(`sdata/S-999000/${sysId}/${item.name}`));
if (cnt1 >= sysIdList.length) {
var cnt2 = 0;
tmpDirList.forEach(tmpDir =>{
ftp.list(`${tmpDir}/${dateString}`, (err, list)=>{
cnt2 ++;
list.forEach(item => needFileList.push(`${tmpDir}/${dateString}/${item.name}`));
if (cnt2 >= tmpDirList.length) {
callback();
}
});
});
}
});
});
});
});
});
});
ftp.connect(connectionProperties);

Node.js使用ftp连接远程ftp服务器枚举和下载文件示例的更多相关文章

  1. Linux下不借助工具实现远程linux服务器上传下载文件

    # Linux下不借助工具实现远程linux服务器上传下载文件 ## 简介 - Linux下自带ssh工具,可以实现远程Linux服务器的功能- Linux下自带scp工具,可以实现文件传输功能 ## ...

  2. Centos下ftp协议连接远程ftp server主机

    环境说明 [root@Check3 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@Check3 ~]# uname -a L ...

  3. Notepad++ 连接远程 FTP 进行文件编辑

    一.下载安装 Notepad++ 1.下载 Notepad++ : https://pan.baidu.com/s/1o7VrS4y 密码 : ck8a 2.安装 Notepad++ 2.1.勾选所有 ...

  4. paip.最好的脚本语言node js 环境搭建连接mysql

    paip.最好的脚本语言node js 环境搭建连接mysql #====下载node...走十一个exe..容易的.. 1 #0----Hello world   .js 2 #---------模 ...

  5. [转]用Node.js创建自签名的HTTPS服务器

    用Node.js创建自签名的HTTPS服务器 创建自己的CA机构 创建服务器端证书 创建客户端证书 将证书打包 创建自己的CA机构 为CA生成私钥 openssl genrsa -out ca-key ...

  6. 前端使用node.js的http-server开启一个本地服务器

    前端使用node.js的http-server开启一个本地服务器 在写前端页面中,经常会在浏览器运行HTML页面,从本地文件夹中直接打开的一般都是file协议,当代码中存在http或https的链接时 ...

  7. 如何配置pl/sql 连接远程oracle服务器

    在下边的两种情况下,如何配置pl/sql 连接远程oracle服务器 1)在客户端不装oracle 客户端也不装服务器,能否配置pl/sql 连接远程oracle服务器,如何配置,请给出详细的文档说明 ...

  8. Node.js创建自签名的HTTPS服务器

    https://cnodejs.org/topic/54745ac22804a0997d38b32d  用Node.js创建自签名的HTTPS服务器  发布于 4 年前  作者 eeandrew  6 ...

  9. 搭建linux远程服务器和传输下载文件

    其实,将ubuntu系统设置为服务器很简单,只需要开启ssh服务就可以了.开启了ssh服务以后,其它电脑就可以通过ssh登录你的这台ubuntu服务器.SSH分客户端openssh-client和op ...

随机推荐

  1. pandas 4

    参考资料:https://mp.weixin.qq.com/s/QnxaOrvlWJn6Dr42Ic1CcQ 1  #只选取housing,loan,contac和poutcometest_data[ ...

  2. Django REST framework+Vue 打造生鲜电商项目(笔记十一)

    (form: http://www.cnblogs.com/derek1184405959/p/8886796.html 有修改) 十四.social_django 集成第三方登录 1.申请应用 进入 ...

  3. Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)

    题目链接:https://codeforces.com/contest/1100/problem/E 题意:给出 n 个点 m 条边的有向图,要翻转一些边,使得有向图中不存在环,问翻转的边中最大权值最 ...

  4. jquery判断两次密码不一致

    jquery检测输入密码两次不一样提示 输入密码: <input type="password" name="password1" id="pa ...

  5. HDU 6000 - Wash

    /* HDU 6000 - Wash [ 贪心 ] 题意: L 件衣服,N 个洗衣机,M 个烘干机,给出每个洗衣机洗一件衣服的时间和烘干机烘干一件衣服的时间,问需要的最少时间是多少 分析: 先求出L件 ...

  6. Debian 9.x "stretch" 解决 /etc/rc.local 开机启动问题

    由于某些软件并没有增加开启启动的服务,很多时候需要手工添加,一般我们都是推荐添加命令到 /etc/rc.local 文件,但是 Debian 9 默认不带 /etc/rc.local 文件,而 rc. ...

  7. zabbix数据的时序-

    gj的proxy服务器经过重启之后时序有变化. zabbix数据库中数据的存储是以哪方为准server端还是agent端, 触发事件跟恢复时间反了,本应该恢复的事件在数据库中查询event,得到的事件 ...

  8. loj3120 「CTS2019 | CTSC2019」珍珠

    link .... 感觉自己太颓废了....还是来更题解吧...[话说写博客会不会涨 rp 啊 qaq ? 题意: 有 n 个物品,每个都有一个 [1,D] 中随机的颜色,相同颜色的两个物品可以配对. ...

  9. NOIP刷题

    搜索 [NOIP2013]华容道 最短路+带剪枝的搜索,是一个思维难度比较大的题目. CF1064D Labyrinth 考虑贪心,用双向队列bfs [NOIP2017]宝藏 剪枝搜索出奇迹 题解:h ...

  10. TensorFlow(十):卷积神经网络实现手写数字识别以及可视化

    上代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = inpu ...