Skip to content

Instantly share code, notes, and snippets.

@akolybelnikov
Forked from scriptex/rename.js
Created May 26, 2019 22:01
Show Gist options
  • Save akolybelnikov/3cc7f7d6922db46d21bd4f4665f18522 to your computer and use it in GitHub Desktop.
Save akolybelnikov/3cc7f7d6922db46d21bd4f4665f18522 to your computer and use it in GitHub Desktop.
Rename all files in a folder with NodeJS
const { join } = require('path');
const { readdirSync, renameSync } = require('fs');
const [dir, search, replace] = process.argv.slice(2);
const match = RegExp(search, 'g');
const files = readdirSync(dir);
files
.filter(file => file.match(match))
.forEach(file => {
const filePath = join(dir, file);
const newFilePath = join(dir, file.replace(match, replace));
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