-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.mjs
More file actions
53 lines (46 loc) · 1.46 KB
/
file.mjs
File metadata and controls
53 lines (46 loc) · 1.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
import fs from "node:fs/promises";
import path from "node:path";
//ASYNC FILE ORGANISER
async function readDir(dirpath) {
try {
const directory= await fs.readdir(dirpath,{withFileTypes:true,recursive:true})
for await(const entry of directory){
const extension=await gettingExtension(entry.name)
const directory=await folders(extension)
const oldPath=path.join(dirpath,entry.name)
const newPath=path.join(directory,entry.name)
movingFile(oldPath,newPath)
}
console.log('success in project1');
} catch (error) {
console.log(`failure in project1 ${error}`);
}
}
async function gettingExtension(params) {
try {
const extension=params.split('.').pop()
console.log('success in extension');
return extension
} catch (error) {
console.log(`no extension ${error}`)
}
}
async function folders(params) {
try {
const pathFolder=path.join('project1',params)
await fs.mkdir(pathFolder,{recursive:true})
console.log('success in directory');
return pathFolder
} catch (error) {
console.log(`unable to create directory ${error}`);
}
}
async function movingFile(oldPath,newPath) {
try {
fs.rename(oldPath,newPath)
console.log('sucess moving');
} catch (error) {
console.log(`unable to move file ${error}`);
}
}
readDir('./content/subfolder')