-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.js
59 lines (45 loc) · 1.68 KB
/
rename.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import fs from "fs/promises"
import path from "path"
import { fileURLToPath } from "url"
import fss from "fs"
// import { verbsList } from "./list"
const __dirname = path.dirname(fileURLToPath(import.meta.url))
function changeJsonFileNames(directoryPath) {
console.log("hey nodee")
fs.readdir(directoryPath, (err, files) => {
console.log('reading')
console.log(files)
if (err) {
console.error('Error reading directory:', err);
return;
}
files.forEach(file => {
console.log('heyy'+file)
const filePath = path.join(directoryPath, file);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
try {
const jsonData = JSON.parse(data);
const newFileName = jsonData._id + '.json';
const newFilePath = path.join(directoryPath, newFileName);
fs.rename(filePath, newFilePath, err => {
if (err) {
console.error('Error renaming file:', err);
return;
}
console.log(`Renamed ${filePath} to ${newFilePath}`);
});
} catch (parseErr) {
console.error('Error parsing JSON:', parseErr);
}
});
});
});
}
// Example usage:
const directoryPath = path.join(__dirname, 'storage', 'datasets', 'default');
// console.log(directoryPath)
changeJsonFileNames(directoryPath);