1const fs = require("fs")
2
3fs.rename("./testfile.txt", "./newtestfile.txt", (err) => {
4 if (err) console.log(err)
5})
1
2 var fs = require('fs');
3
4fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
5
6 if (err) throw err;
7 console.log('File Renamed!');
8});
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'