-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenamer.js
More file actions
32 lines (27 loc) · 856 Bytes
/
Copy pathrenamer.js
File metadata and controls
32 lines (27 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const fs = require('fs')
const path = require('path')
function renameAllInDir(dir, renameDirs = false) {
let files = []
fs.readdirSync(folder).forEach(file => {
files.push(file)
});
for (let i = 0; i < files.length; i++) {
// Skips dirs if needed
if (!renameDirs) {
if (path.extname(files[i] === ''))
continue;
}
let fileName = files[i]
let filePath = path.join(folder, fileName)
let newName = i + path.extname(fileName)
let newPath = path.join(folder, newName)
fs.rename(filePath, newPath, function (err) {
if (err) {
console.log('ERROR: ' + err);
throw err;
}
console.log(`${fileName} renamed to ${newName}`);
});
}
}
module.exports = { renameAllInDir }