Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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++
}

>Solution :

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++
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading