Skip to content

Instantly share code, notes, and snippets.

@paddingme
Forked from scriptex/rename.js
Created April 29, 2019 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paddingme/27d8ffd8581be50e0dd48888f726e77a to your computer and use it in GitHub Desktop.
Save paddingme/27d8ffd8581be50e0dd48888f726e77a to your computer and use it in GitHub Desktop.
Rename all files in a folder with NodeJS
const fs = require('fs');
const path = require('path');
const args = process.argv.slice(2);
const dir = args[0];
const match = RegExp(args[1], 'g');
const replace = args[2];
const files = fs.readdirSync(dir);
files
.filter(file => file.match(match))
.forEach(file => {
const filePath = path.join(dir, file);
const newFilePath = path.join(dir, file.replace(match, replace));
fs.renameSync(filePath, newFilePath);
});
// Usage
// node rename.js path/to/directory 'string-to-search' 'string-to-replace'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment