Skip to content

Instantly share code, notes, and snippets.

@yauri-io
Last active February 1, 2020 16:08
Show Gist options
  • Save yauri-io/e7c0f26e811c5eb8aacc2c27bdbf6b29 to your computer and use it in GitHub Desktop.
Save yauri-io/e7c0f26e811c5eb8aacc2c27bdbf6b29 to your computer and use it in GitHub Desktop.
Renaming all files inside a folder (MacOS) using NodeJS
'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