博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件命名为node.js_如何在Node.js中批量重命名文件
阅读量:2509 次
发布时间:2019-05-11

本文共 2665 字,大约阅读时间需要 8 分钟。

文件命名为node.js

In this blog post I’m going to explain how to rename a set of files.

在这篇博客中,我将解释如何重命名一组文件。

The same process works to move files to another folder, because when you rename you rename the path of the file.

相同的过程可以将文件移动到另一个文件夹,因为重命名时会重命名文件的路径。

The motivation for this task was this: in Hugo we can write blog posts as files, like this:

这项任务的动机是这样的:在Hugo中,我们可以将博客文章作为文件编写,如下所示:

first-post.mdsecond-post.mdthird-post.md

We can also add them to a folder that contains an index.md file:

我们还可以将它们添加到包含index.md文件的文件夹中:

first-post/  > index.mdsecond-post/  > index.mdthird-post/  > index.md

The difference is that with folder we can add images and associate them more easily to a blog post.

区别在于,通过文件夹,我们可以添加图像并将其更轻松地关联到博客文章。

I could have done the change manually, but I had about 50 files in the folder and I didn’t really want to do that job.

我本可以手动进行更改,但是该文件夹中大约有50个文件,我真的不想做这项工作。

I wanted it to happen automatically.

我希望它能够自动发生。

Let’s start by requiring one core module we’re going to use: fs. As it is a core module, there’s no need to npm install it.

首先,我们需要一个将要使用的核心模块: fs 。 由于它是核心模块,因此不需要npm install它。

const fs = require('fs')

Then, get a reference to the current folder. I suppose we’re going to run the script in the same folder where we want to perform this change.

然后,获取对当前文件夹的引用。 我想我们将在要执行此更改的文件夹中运行脚本。

__dirname is the variable that always points to the current working folder.

__dirname是始终指向当前工作文件夹的变量。

I get a list of all the files and folders:

我得到所有文件和文件夹的列表:

const files = fs.readdirSync(__dirname)

Then I filter out only the items that end with .md:

然后,我仅过滤出以.md结尾的项目:

for (const file of files) {  if (file.endsWith('.md')) {    console.log(file)  }}

Once we have the file reference, which represents the filename, we can call fs.rename().

一旦有了代表文件名的file引用,就可以调用fs.rename()

This function accepts 3 parameters:

此函数接受3个参数:

  1. the current path

    当前路径
  2. the path we want to move to

    我们要移动的路径
  3. an callback fired if there’s an error

    发生错误时触发的回调

The current path is:

当前路径是:

__dirname + '/' + item

The path we want to move to is:

我们要移动的路径是:

__dirname + '/' + item.replace('.md', '') + '/index.md'

See? We create a new folder from the file name, then we append index.md:

看到? 我们从文件名创建一个新文件夹,然后添加index.md

fs.rename(  __dirname + '/' + item,  __dirname + '/' + item.replace('.md', '') + '/index.md',  err => {    console.log(err)  })

Here’s the full code:

这是完整的代码:

const fs = require('fs')const files = fs.readdirSync(__dirname)for (const file of files) {  if (file.endsWith('.md')) {    fs.rename(      __dirname + '/' + item,      __dirname + '/' + item.replace('.md', '') + '/index.md',      err => {        console.log(err)      }    )  }}

翻译自:

文件命名为node.js

转载地址:http://namgb.baihongyu.com/

你可能感兴趣的文章
MVC 前台向后台传输数据
查看>>
《少年先疯队》第四次作业:项目需求调研与分析
查看>>
IPv6 Scapy Samples
查看>>
Asp.Net Ajax的两种基本开发模式
查看>>
哈希——并查集结构——岛问题
查看>>
正则表达式
查看>>
图像处理笔记(十二)
查看>>
条件数(condition number)
查看>>
Chapter 3 Phenomenon——9
查看>>
win64 Python下安装PIL出错解决2.7版本 (3.6版本可以使用)
查看>>
获取各种类型的节点
查看>>
表达式求值-201308081712.txt
查看>>
centos中安装tomcat6
查看>>
从Vue.js窥探前端行业
查看>>
学习进度
查看>>
poj3368 RMQ
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>