fs renaming files
const fs = require("fs")
fs.rename("./testfile.txt", "./newtestfile.txt", (err) => {
if (err) console.log(err)
})
fs renaming files
const fs = require("fs")
fs.rename("./testfile.txt", "./newtestfile.txt", (err) => {
if (err) console.log(err)
})
Rename files in a directory with node.js
const fs = require("fs");
const path = require("path");
const folderPath = "./assets";
// read all files in the directory
let filesArr = fs.readdirSync(folderPath);
// Loop through array and rename all files
filesArr.forEach((file, index) => {
let fullPath = path.join(folderPath, file);
let fileExtension = path.extname(file);
let fileName = path.basename(file, fileExtension);
let newFileName = fileName + index + "." + fileExtension;
try {
fs.renameSync(fullPath, path.join(folderPath, newFileName));
} catch (error) {
console.error(error)
}
});
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us