-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
110 lines (97 loc) · 2.4 KB
/
app.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
102
103
104
105
106
107
108
109
110
const path = require('path');
const { join } = path;
const { readdirSync, renameSync } = require('fs');
//files directory cmd line argument
const [fileDir] = process.argv.slice(2);
//file extension cmd line argument
const [fileExt] = process.argv.slice(3);
//files to be replaced
const files = readdirSync(fileDir);
//filenames you want to replace to the files
const arrFileNames = ['renamesample',
'bruhname',
'samplerename',
]
const fileExtTypes = [
"aiff",
"aif",
"au",
"avi",
"bat",
"bmp",
"class",
"java",
"csv",
"cvs",
"dbf",
"dif",
"doc",
"docx",
"eps",
"exe",
"fm3",
"gif",
"hqx",
"htm",
"html",
"jpg",
"jpeg",
"mac",
"map",
"mdb",
"mid",
"midi",
"mov",
"qt",
"mtb",
"mtw",
"pdf",
"p65 .t65",
"png",
"ppt",
"pptx",
"psd",
"psp",
"qxd",
"ra",
"rtf",
"sit",
"tar",
"tif",
"txt",
"wav",
"wk3",
"wks",
"wpd",
"wp5",
"xls",
"xlsx",
"zip"
]
for (var i = 0; i < files.length; i++) {
//if the files that has been read on the directory is greater than the filenames array
if (arrFileNames.length > i) {
//get the file extension
const ext = files[i].substring(files[i].lastIndexOf('.') + 1);
const extNameOfFileName = arrFileNames[i].substring(arrFileNames[i].lastIndexOf('.') + 1);
var oldPath = "",
newFilePath = "";
if (fileExt == ext) {
//if one or all of the given filenames on the array is not on the fileExtTypes
//then give it a file extension that has been passed from the command line argument
if (fileExtTypes.indexOf(extNameOfFileName) != -1) {
oldPath = fileDir + "/" + files[i];
newFilePath = join(fileDir, arrFileNames[i]);
} else {
oldPath = fileDir + "/" + files[i];
newFilePath = join(fileDir, arrFileNames[i] + "." + fileExt);
}
//rename it
renameSync(oldPath, newFilePath);
} else {
console.log(`The extension ${fileExt} you provided is not the same with expected file extension ${ext}`)
}
} else {
console.log(`The files the has been read on the directory has length of ${files.length} and the filenames array length is ${arrFileNames.length}`)
}
}