摘要:创建时间注意在,环境运行无问题首先引入相关包会在使用处具体说明该包为实验性对文件的操作复制文件这里列出三种方式使用和结合使用使用的方法其中的同步或异步方法可酌情更改,实现代码如下被复制文件的地址,相对地址放置复制文件的地址,相对地址方
[toc]
创建时间:2019-08-12注意:在win10,v10.16.1 环境运行无问题
首先引入相关包(会在使用处具体说明):
const fs = require("fs")
const path = require("path")
const child_process = require("child_process")
const fsEx = require("fs-extra")
/**
* @des 该包为实验性API
*/
const fsPromises = require("fs").promises
对文件的操作
复制文件
这里列出三种方式:
使用 writeFileSync 和 readFileSync 结合
使用 copyFileSync
使用promises的copyFile方法
其中的同步或异步方法可酌情更改,实现代码如下
/**
* @param { copiedPath: String } (被复制文件的地址,相对地址)
* @param { resultPath: String } (放置复制文件的地址,相对地址)
*/
function copyFile(copiedPath, resultPath) {
copiedPath = path.join(__dirname, copiedPath)
resultPath = path.join(__dirname, resultPath)
try {
/**
* @des 方式一
*/
// fs.writeFileSync(resultPath, fs.readFileSync(copiedPath))
/**
* @des 方式二
*/
// fs.copyFileSync(copiedPath, resultPath)
console.log("success");
} catch (error) {
console.log(error);
}
/**
* @des 方式三
*/
fsPromises.copyFile(copiedPath, resultPath)
.then(() => {
console.log("success");
}).catch((err) => {
console.log(err);
});
}
删除文件
使用 unlinkSync 方法,实现代码如下
/**
* @param { delPath:String } (需要删除文件的地址)
* @param { direct:Boolean } (是否需要处理地址)
*/
function deleteFile(delPath, direct) {
delPath = direct ? delPath : path.join(__dirname, delPath)
try {
/**
* @des 判断文件或文件夹是否存在
*/
if (fs.existsSync(delPath)) {
fs.unlinkSync(delPath);
} else {
console.log("inexistence path:", delPath);
}
} catch (error) {
console.log("del error", error);
}
}
对文件夹(目录)的操作
以下代码有引用,复制文件相关方法
复制文件夹使用了两种方式:
child_process
递归的读取文件和文件夹再在指定地址创建
实现代码和释意如下:
/**
* @des 参数解释同上
*/
function copyFolder(copiedPath, resultPath, direct) {
if(!direct) {
copiedPath = path.join(__dirname, copiedPath)
resultPath = path.join(__dirname, resultPath)
}
function createDir (dirPath) {
fs.mkdirSync(dirPath)
}
if (fs.existsSync(copiedPath)) {
createDir(resultPath)
/**
* @des 方式一:利用子进程操作命令行方式
*/
// child_process.spawn("cp", ["-r", copiedPath, resultPath])
/**
* @des 方式二:
*/
const files = fs.readdirSync(copiedPath, { withFileTypes: true });
for (let i = 0; i < files.length; i++) {
const cf = files[i]
const ccp = path.join(copiedPath, cf.name)
const crp = path.join(resultPath, cf.name)
if (cf.isFile()) {
/**
* @des 创建文件,使用流的形式可以读写大文件
*/
const readStream = fs.createReadStream(ccp)
const writeStream = fs.createWriteStream(crp)
readStream.pipe(writeStream)
} else {
try {
/**
* @des 判断读(R_OK | W_OK)写权限
*/
fs.accessSync(path.join(crp, ".."), fs.constants.W_OK)
copyFolder(ccp, crp, true)
} catch (error) {
console.log("folder write error:", error);
}
}
}
} else {
console.log("do not exist path: ", copiedPath);
}
}
删除文件夹
递归文件和文件夹,逐个删除
实现代码如下:
function deleteFolder(delPath) {
delPath = path.join(__dirname, delPath)
try {
if (fs.existsSync(delPath)) {
const delFn = function (address) {
const files = fs.readdirSync(address)
for (let i = 0; i < files.length; i++) {
const dirPath = path.join(address, files[i])
if (fs.statSync(dirPath).isDirectory()) {
delFn(dirPath)
} else {
deleteFile(dirPath, true)
}
}
/**
* @des 只能删空文件夹
*/
fs.rmdirSync(address);
}
delFn(delPath);
} else {
console.log("do not exist: ", delPath);
}
} catch (error) {
console.log("del folder error", error);
}
}
执行示例
目录结构
|- index.js(主要执行代码)
|- a
|- a.txt
|- b.txt
|- c
|- a.txt
|- b.txt
|- p
|- a.txt
|- b.txt
根据传入的参数不同,执行相应的方法
/**
* @des 获取命令行传递的参数
*/
const type = process.argv[2]
function execute() {
/**
* @des 请根据不同的条件传递参数
*/
if (type === "copyFile") {
copyFile("./p/a.txt", "./c/k.txt")
}
if (type === "copyFolder") {
copyFolder("./p", "./a")
}
if (type === "delFile") {
deleteFile("./c/ss.txt")
}
if (type === "delFolder") {
deleteFolder("./a")
}
}
execute()
命令行传参数
/**
* @des 命令行传参
* 执行 node ./xxx/index.js 111 222
* 输出:
* 0: C:Program Files
odejs
ode.exe
* 1: G:GitHubxxxxxxxindex.js
* 2: 111
* 3: 222
*/
process.argv.forEach((val, index) => {
console.log(`${index}: ${val}`);
});
利用 fs-extra 实现
这是对fs相关方法的封装,使用更简单快捷
/**
* @des fs-extra 包实现
* api参考: https://github.com/jprichardson/node-fs-extra
*/
function fsExtra() {
async function copy() {
try {
await fsEx.copy(path.join(__dirname + "/p"), path.join(__dirname + "/d"))
console.log("success");
} catch (error) {
console.log(error);
}
}
copy()
}
可执行源码: https://github.com/NameHewei/node-koa/tree/master/moveFileOrFloder
欢迎交流 Github
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/106660.html
摘要:是针对和文档的一个,描绘了一个层次化的节点树,允许开发人员添加修改删除节点的一部分。类型级定义了接口,该接口由中的所有节点类型实现。添加的这些属性分别对应于每个元素中都存在的下列标准特性。 DOM是针对HTML和XML文档的一个API,描绘了一个层次化的节点树,允许开发人员添加、修改、删除节点的一部分。 DOM将HTML和XML文档描绘成一个有多个节点构成的结构,节点分为12种不同的...
摘要:分布式架构原理设计的理念就是分布式搜索引擎,底层实现还是基于的,核心思想是在多态机器上启动多个进程实例,组成一个集群。 es分布式架构原理 elasticsearch设计的理念就是分布式搜索引擎,底层实现还是基于Lucene的,核心思想是在多态机器上启动多个es进程实例,组成一个es集群。一下是es的几个概念: 接近实时es是一个接近实时的搜索平台,这就意味着,从索引一个文档直到文档...
在经历了6,7个项目同时开工,频繁发布测试 ,不得不学会一点偷懒的小技巧来提高效率了,所以这篇文章要讲的就是如何更加优化发布流程。 工作以来,经历了build后,然后用FileZilla上传服务器完成部署。再到前端打包后 ,在build仓库执行git push,后端在自动部署。后端的自动部署的确简化了很多操作,不过对于前端来说 ,每次发布还需要去build仓库执行push操作,特别是发布频繁的时候...
摘要:类型对象是的一个实例,表示整个页面,而且,对象是对象的一个属性,因此可以将其作为全局对象来访问。删除指定位置的行。创建创建创建第一行创建第二行将表格添加到文档主体中 DOM 节点层次 Node类型 DOM1级定义了一个Node接口,该接口将由DOM中的所有节点类型实现 节点类型由在Node类型中定义的12个数值常量来表示,任何节点类型必居其一 Node.ELEMENT_NODE(...
阅读 7344·2021-09-22 15:08
阅读 2423·2021-08-24 10:03
阅读 2759·2021-08-20 09:36
阅读 1757·2020-12-03 17:22
阅读 2738·2019-08-30 15:55
阅读 1245·2019-08-29 16:13
阅读 3319·2019-08-29 12:41
阅读 3525·2019-08-26 12:12