Home Web Front-end JS Tutorial How to use node to implement a function for batch renaming files

How to use node to implement a function for batch renaming files

Dec 21, 2017 pm 03:16 PM
node accomplish

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 detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it Let’s learn with the editor below.

Implementation ideas

After sorting out the idea, it is very simple. First, read the file name of the original file and put it into an array, and then use the renameAPI to implement the new name as an array. However, The names for batch renaming can only follow the rule of number + 1. The following function is written

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('done!')
  })
 }
 })
}
rename()
Copy after login

File directory

Place the file to be renamed in the file folder

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

This is just a simple implementation, or There are many deficiencies, there are better methods, welcome to discuss

Related recommendations:

Introduction to the implementation method of batch renaming all files in a folder in PHP

php batch rename_PHP tutorial

Python method to batch rename files in a specified directory

The above is the detailed content of How to use node to implement a function for batch renaming files. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones?

Use Java to write code to implement love animation Use Java to write code to implement love animation Dec 23, 2023 pm 12:09 PM

Use Java to write code to implement love animation

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

PHP Programming Guide: Methods to Implement Fibonacci Sequence

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node?

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

Master how Golang enables game development possibilities

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide

See all articles