Skip to content
Advertisement

FS rename file – Error: ENOENT: no such file or directory, rename ’24.png’ -> ‘1.png’

I am trying to write a little script that will rewrite the file names of the images in my folder.

Where am I going wrong?

I am getting this error: FS rename file – Error: ENOENT: no such file or directory, rename ’24.png’ -> ‘1.png’

const fs = require('fs');

const fileNames = fs.readdirSync('./images')

for(const fileName of fileNames) {
    const ext = '.png'
    let incNumber = 1;
    let newName = String(incNumber + ext);
    fs.renameSync(fileName, newName);
    incNumber++
}

Advertisement

Answer

You do not need to change number to string while adding a string into number. If you want to be sure you can call incNumber.toString() + ext

const fs = require('fs');
const path = require("path");
const fileNames = fs.readdirSync('./images')
const ext = '.png'
let incNumber = 1;
for(const fileName of fileNames) {
    let newName = path.join('./images', incNumber + ext);
    fs.renameSync(path.join('./images',fileName), newName);
    incNumber++
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement