-
Notifications
You must be signed in to change notification settings - Fork 0
/
renameMyfiles.js
101 lines (88 loc) · 3.55 KB
/
renameMyfiles.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import path from 'path';
import fs from 'fs';
const root = './ter'
//joining path of directory
const ownerName = ''
const owners = ['Henok m☻vies']
function goToDirectory(directoryPath) {
fs.readdir(directoryPath, function (err, files) {
if (err) {
console.log(err)
}
else {
for (const file of files) {
const filePath = `${directoryPath}\\${file}`
fs.stat(filePath, async (err, stats) => {
if (err) {
} else {
if (stats.isFile()) {
// replace replace owner name
let newFileName = ""
if (file.includes(owners[0])) {
newFileName = file.replace(owners[0], ownerName)
}
//add owner name
else {
const indexOfDot = file.lastIndexOf('.')
const afterDot = file.slice(indexOfDot)
const beforeDot = file.slice(0, indexOfDot)
newFileName = `${beforeDot} ${ownerName}${afterDot}`
}
try {
renameFileName(directoryPath, file, newFileName)
} catch (error) {
console.log(`\n${error}\n`)
}
} else if (stats.isDirectory()) {
let newFolderName = ""
// replace owner name
if (file.includes(owners[0])) {
newFolderName = file.replace(owners[0], ownerName)
}
//add owner name
else{
newFolderName = `${file} ${ownerName}`
}
try {
const folderPath = await renameDirectoryName(directoryPath, file, newFolderName)
goToDirectory(folderPath);
} catch (error) {
console.log(error)
}
} else {
}
}
})
}
}
})
}
goToDirectory('C:\\Users\\deep\\Documents\\## romance');
function renameDirectoryName(folderPath, oldFolderName, newFolderName) {
return new Promise(function (resolve, reject) {
fs.rename(`${folderPath}\\${oldFolderName}`, `${folderPath}\\${newFolderName}`, err => {
if (err) {
console.log(`\nfailure in renaming ${oldFolderName} to :\t ${newFolderName}`)
reject(err)
}
else {
console.log(`\nSuccessfully renamed from ${oldFolderName} to :\t ${newFolderName}`)
resolve(`${folderPath}\\${newFolderName}`)
}
})
}).then(result => {
return result
}).catch(err => {
return err
})
}
function renameFileName(filePath, oldFileName, newFileName) {
fs.rename(`${filePath}/${oldFileName}`, `${filePath}/${newFileName}`, err => {
if (err) {
console.log(`\nError in renaming ${oldFileName} to :\t ${newFileName}`)
}
else {
console.log(`\nSuccessfully renamed from ${oldFileName} to :\t ${newFileName}`)
}
})
}