-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
51 lines (48 loc) · 1.56 KB
/
main.js
File metadata and controls
51 lines (48 loc) · 1.56 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
const fs = require("fs")
var paths = []
var filePaths = []
var fileName = []
// Détection des Dossiers
async function loadDirs(path = "./files") {
const dirs = await fs.promises.opendir(path)
for await (const dir of dirs) {
if(dir.isDirectory()) {
if(!paths.includes(`${path}/${dir.name}`)) {
loadDirs(`${path}/${dir.name}`)
paths.push(`${path}/${dir.name}`)
console.log(paths[paths.length - 1])
}
} else loadFiles()
}
}
// Détection des Fichiers
async function loadFiles() {
for(var i = 0; i < paths.length; i++) {
const files = await fs.promises.opendir(paths[i])
for await (const file of files) {
if(file.isFile()) {
if(!filePaths.includes(`${paths[i]}/${file.name}`)) {
filePaths.push(`${paths[i]}/${file.name}`)
fileName.push(file.name)
console.log(filePaths[filePaths.length - 1])
sortFile(filePaths.length - 1)
}
} else loadDirs()
}
}
}
// Tri des Fichiers
function sortFile(y) {
fs.stat(filePaths[y], function(err, stats){
var date = stats.mtime.toDateString()
fs.mkdir(`./sort/${date}`, function (err) {
if(!err || (err && err.code === 'EEXIST')) {
fs.copyFile(filePaths[y], `./sort/${date}/${fileName[y]}`, function(err) {
if(err) console.log(err)
})
}
})
})
}
console.log("Sort Initialized...")
loadDirs()