Last active
August 14, 2018 14:55
-
-
Save kostandy/deba370b601ddc1f3a40521bb0ac1939 to your computer and use it in GitHub Desktop.
This little script allow you rename files in folder by list of names from any file which contains text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); // Include File System Module to project | |
let listOfNames; // Initialization of variable | |
let pathToListOfNames = './example.txt'; // Path to text file with list of names | |
let pathToFiles = './media/images'; // Path to files which need to rename | |
let formatFiles = 'jpg'; // Specify your image format (Example: jpg, png, gif, etc.) | |
try { | |
listOfNames = fs.readFileSync(pathToListOfNames, 'utf-8') | |
.split('\n'); // Reading a file by strings and splitting it into an array by '\n' | |
} catch (ex) { | |
console.error(ex); // Display an error if it exists | |
} | |
listOfNames.forEach( // Iterates through the array | |
(name, i) => { | |
try { | |
fs.renameSync(`${pathToFiles}/${i+1}.${formatFiles}`, `${pathToFiles}/${name}.${formatFiles}`); // Renaming files which has name: 1.jpg, 2.jpg etc. | |
} catch (ex) { | |
console.error(ex); // Display an error if it exists | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment