-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory-handling.mjs
More file actions
101 lines (97 loc) · 3.46 KB
/
directory-handling.mjs
File metadata and controls
101 lines (97 loc) · 3.46 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
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 os from "node:os";
import fs from "node:fs";
import path from "node:path";
import { readdir, rm } from "node:fs/promises";
import { log } from "node:console";
const folderPath=path.join(os.homedir(),'Downloads')
async function read(params) {
try {
const allFiles=await readdir(params,{encoding:'utf-8',withFileTypes:true,recursive:true})
const filtterdFiles= allFiles.filter((elem)=>elem.isFile())
return filtterdFiles
} catch (error) {
throw error
}
}
const allFiles= await read(folderPath)
//console.log(allFiles);
function extensions(params) {
const extension=params.name.split('.').pop()
extension.toLowerCase()
return extension
}
async function directory() {
const videos=path.join(folderPath,"videos")
const pictures=path.join(folderPath,"pictures")
const webdev=path.join(folderPath,"webdev")
const docs=path.join(folderPath,"docs")
const others=path.join(folderPath,"others")
const folders= new Map()
folders.set('videos',videos);
folders.set('pictures',pictures);
folders.set('webdev',webdev);
folders.set('docs',docs);
folders.set('others',others);
for (const [key,value] of folders) {
fs.mkdir(value,{recursive:true},err=>{
if (err) {
console.log(`caused by ${value} ${err}`);
}
})
}
return folders
}
async function folderOrganiser() {
const videos=["mp4", "mkv", "mov", "avi", "wmv", "FLV", "WebM", "3GP","H.264", "H.265", "VP8", "VP9", "AV1", "MPEG-2", "MPEG-4", "Theora","MXF", "ProRes", "DNxHD"]
const webdev=['js','json','mjs','css','html']
const pictures= ["JPEG", "PNG", "GIF", "BMP", "TIFF", "WebP", "HEIF", "HEIC","SVG", "EPS","RAW", "ICO", "PSD"];
const docs=['docx','txt','doc','ppt','pdf']
const folders=await directory()
allFiles.forEach(elem=>{
const oldPath=path.join(elem.path,elem.name)
const exten=extensions(elem)
if (videos.includes(exten)) {
const value=folders.get('videos')
const newPath=path.join(value,elem.name)
fs.rename(oldPath,newPath,err=>{
if (err) {
console.log(`---${err}---${elem} `);
}
})
}else if (webdev.includes(exten)) {
const value=folders.get('webdev')
const newPath=path.join(value,elem.name)
fs.rename(oldPath,newPath,err=>{
if (err) {
console.log(`---${err}---${elem} `);
}
})
}else if (pictures.includes(exten.toLowerCase())) {
const value=folders.get('pictures')
const newPath=path.join(value,elem.name)
fs.rename(oldPath,newPath,err=>{
if (err) {
console.log(`---${err}---${elem} `);
}
})
}else if (docs.includes(exten)) {
const value=folders.get('docs')
const newPath=path.join(value,elem.name)
fs.rename(oldPath,newPath,err=>{
if (err) {
console.log(`---${err}---${elem} `);
}
})
}else{
const value=folders.get('others')
const newPath=path.join(value,elem.name)
fs.rename(oldPath,newPath,err=>{
if (err) {
console.log(`---${err}---${elem} `);
}
})
}
})
console.log('done');
}
folderOrganiser()