Last active
February 1, 2020 16:08
-
-
Save yauri-io/e7c0f26e811c5eb8aacc2c27bdbf6b29 to your computer and use it in GitHub Desktop.
Renaming all files inside a folder (MacOS) using NodeJS
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
'use strict' | |
const Fs = require('fs') | |
const Path = require('path') | |
const filePath = process.env.filePath | |
if (!filePath) { | |
throw Error ('Please set filePath!') | |
} | |
const getRandomStr = () => Math.random().toString(36).substring(2, 30) + Math.random().toString(36).substring(2, 30); | |
const getExtension = (fileName) => Path.extname(fileName) | |
Fs.readdirSync(filePath).forEach(file => { | |
const currentFile = `${filePath}/${file}` | |
const newFile = `${filePath}/${getRandomStr()}${getExtension(file)}` | |
Fs.renameSync(currentFile, newFile); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment