示例代码:

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. 六、用DataContractSerialize类序列化XML

    一.层次结构 基类:XmlObjectSerializer 派生类: DataContractSerializer NetDataContractSerializer DataContractJson ...

  2. 创建型模式(一) 单例模式(Singleton)

    一.动机(Motivation) 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性.以及良好的效率. 如何绕过常规的构造器,提供一种机制来保证一个类只 ...

  3. MySQL5.7版本安装(压缩包形式)

    1.去官网下载 MySQL 压缩包 2.配置环境变量 3.创建配置文件my.ini (放置 mysql-5.7.28-winx64 目录下) my.ini 配置文件 编写如下内容 [client] p ...

  4. cc 视频的使用

    1. 先上传视频 2.复制代码 3.贴在页面上就可以使用了 4.通过id指定播放那个视频

  5. HDU-1398-Square Coins(母函数)

    链接: https://vjudge.net/problem/HDU-1398 题意: People in Silverland use square coins. Not only they hav ...

  6. 使用JSP/Servalet技术开发新闻发布系统------动态网页开发基础

    什么是动态网页? 动态网页是指在服务器端运行的程序或者网页,它们会随不同客户.不同时间,返回不同的网页. 动态网页的特点? (1).交互性:即网页会根据用户的要求和选择而动态改变和响应.采用动态网页技 ...

  7. IE下iframe不能正常加载,显示空白

    下午帮忙看了一个web问题,index.html中嵌入<iframe>来加载同文件目录下的一个页面,在多个浏览器下测试,发现IE浏览器中会出现问题,<iframe>不能正常加载 ...

  8. Hive的安装和配置

    前提是:hadoop必须已经启动了***         1°.解压hive的安装包            [crxy@master soft]# tar -zxvf apache-hive-0.14 ...

  9. KindEditor3.x-自动上传Word图片功能.

    Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...

  10. Python多线程笔记(二)

    Lock对象 原语锁(互斥锁)是一个同步原语,状态是"已锁定"或者"未锁定"之一.两个方法acquire()和release()用于修改锁的状态.如果状态为已锁 ...