-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (45 loc) · 1.38 KB
/
index.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
//import path, fs and shelljs modules
const path = require('path');
const fs = require('fs');
const shell = require('shelljs');
//get the current working directory
var dirPath = process.cwd();
//array to store the extension names
var fileType = [];
//reading dir
fs.readdir(dirPath,function(err,files){
//handling error
if(err){
return console.log('Unable to scan directory: ', err);
}
//array for storing extensions
var fileExt=[]; //contains duplicate elements too
var fileType=[]; //only contains unique elements
//copying extname in fileExt
for(var i=0;i<files.length;i++){
fileExt[i]=path.extname(files[i]);
}
//removing empty string from array
fileExt = fileExt.filter(Boolean)
//pushing unique values in fileType and creating folders for unique extensions
for(var value of fileExt){
if(fileType.indexOf(value)===-1){
fileType.push(value);
try {
if (!fs.existsSync("All_"+value)){
fs.mkdirSync("All_"+value);
}
} catch (err) {
console.error(err)
}
}
}
//move files into their respective folder
for(var value of fileType){
for(var val of files){
if(value==path.extname(val)){
shell.mv(val,"All_"+value);
}
}
}
});