-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
44 lines (35 loc) · 1.06 KB
/
Copy pathmain.js
File metadata and controls
44 lines (35 loc) · 1.06 KB
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
const fs = require('fs');
const path = require('path');
let dirpath = path.join(__dirname, "folder")
let a = fs.readdirSync(dirpath)
let folders = [];
let ext;
let newdirpath;
for (let i = 0; i < a.length; i++) {
const element = a[i];
ext = path.extname(element);
newdirpath = path.join(dirpath, ext.slice(1));
if (!folders.includes(newdirpath)) {
folders.push(newdirpath)
}
}
for (let i = 0; i < folders.length; i++) {
const element = folders[i];
fs.mkdirSync(element, { recursive: true });
}
let fileNames = fs.readdirSync(dirpath);
let files = [];
for (let i = 0; i < fileNames.length; i++) {
const fileName = fileNames[i];
const filePath = path.join(dirpath, fileName);
if (fs.statSync(filePath).isFile()) {
files.push(fileName);
}
}
// Moving the files to appropriate folders
for (let i = 0; i < files.length; i++) {
const element = files[i];
const filePath = path.join(dirpath, element)
const fileExt = path.extname(filePath).slice(1)
fs.renameSync(filePath, `${dirpath}/${fileExt}/${element}`)
}