Home > Web Front-end > JS Tutorial > body text

How to implement the rename file function using node

亚连
Release: 2018-06-19 14:19:30
Original
1759 people have browsed it

This article mainly introduces you to the relevant information about using node to implement a function of batch renaming files. The article introduces it in great detail through sample code. It has certain reference learning value for everyone's study or work. It is needed Friends, let’s study together.

Preface

Recently while having lunch, I suddenly remembered that when I was taking a JS course at SMU, the teacher mentioned something when he was talking about node. It was said that node can rename files in batches, so I wanted to see if this function could be implemented.

After reading the official documentation of node, I found that the fs module has a readdir API, which reads the contents of a directory. After testing it, the returned result is an array, and the element is the name of the folder. For a detailed introduction, please refer here: //www.jb51.net/article/58609.htm

There is also an API, rename is related to renaming by listening to the name. For details, please refer here: //www.jb51.net/article/58548.htm

Implementation ideas

It’s very easy to sort out the ideas Simple, just read the file name of the original file and put it into an array, and then use the rename API to implement the new name as an array. However, the names for batch renaming can only follow the rule of the number 1. I wrote the following function

Sample code

//rename.js
const fs = require('fs') //引入node内置的文件系统

function rename() {
 let newName = []
 fs.readdir('./file/', (err, oldName) => { //读取file文件夹下的文件的名字,oldName是一个数组
 if (err) {
  console.log(err)
 }
 for (let i = 0; i < oldName.length; i++) {
  let name = `new${i}.jpg` // 以图片为例
  newName[i] = name  // 把名字赋给一个新的数组
 }
 for (var i = 0; i < oldName.length; i++) {
  let oldPath = `./file/${oldName[i]}` //原本的路径
  let newPath = `./file/${newName[i]}` //新路径
  fs.rename(oldPath, newPath, (err) => { //重命名
  if (err) {
   console.log(err)
  }
  console.log(&#39;done!&#39;)
  })
 }
 })
}
rename()
Copy after login

File directory

file file Place the file to be renamed in the folder

Open the terminal, cd to the rename folder, and execute node rename.js

This is just a simple implementation. There are still many shortcomings, and there are better methods. Welcome to discuss

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to implement finger sliding carousel on mobile terminal in js

How to implement parent component orientation in vue Subcomponents pass data

How to use ajax in wordpress

How to implement keyword fuzzy query in jq.ajax php mysql

How to dynamically modify the page title in Vue

The above is the detailed content of How to implement the rename file function using node. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template