1const fs = require("fs")
2
3fs.rename("./testfile.txt", "./newtestfile.txt", (err) => {
4 if (err) console.log(err)
5})
1const { join } = require('path');
2const { readdirSync, renameSync } = require('fs');
3const [dir, search, replace] = process.argv.slice(2);
4const match = RegExp(search, 'g');
5const files = readdirSync(dir);
6
7files
8 .filter(file => file.match(match))
9 .forEach(file => {
10 const filePath = join(dir, file);
11 const newFilePath = join(dir, file.replace(match, replace));
12
13 renameSync(filePath, newFilePath);
14 });
15
16// Usage
17// node rename.js path/to/directory 'string-to-search' 'string-to-replace'