-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortFilesIntoFolder.js
More file actions
68 lines (62 loc) · 2.03 KB
/
sortFilesIntoFolder.js
File metadata and controls
68 lines (62 loc) · 2.03 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
const fse = require('fs-extra');
const jsonFilter = new RegExp('.json$')
var files = (fse.readdirSync('./Keep')).filter(e => e.match(jsonFilter));
// copy all notes to a single folder
async function copyAllJsonNotesToASeparateFolder(){
fse.ensureDir("./notes/", async (e, s) => {
if(!e){
for(let fileName of files){
try{
await fse.copyFile(`./keep/${fileName}`, `./notes/${fileName}`);
} catch(e){
console.log(e)
}
}
}
});
}
// moving files to folders
async function moveAllNotesToFolders(){
for(const fileName of files) {
try{
// await copyAllJsonNotesToASeparateFolder();
let note = await fse.readJson( `./notes/${fileName}`);
if(note.labels){
for(let label of note.labels){
console.log(label.name)
//create folder for notes with label
await fse.ensureDir(`./notes/${label.name}`)
await fse.move(`./notes/${fileName}`, `./notes/${label.name}/${fileName}`)
}
}
else {
console.log(note.title)
// create separate folder for misc notes
await fse.ensureDir(`./notes/misc`)
await fse.move(`./notes/${fileName}`, `./notes/misc/${fileName}`)
}
} catch(e){
console.error(e)
}
};
}
// creating array of all the files in heirarchy
async function checkIfALLAreCopied(root = "./notes", current = "", arr = []){
const state = await fse.statSync(root);
if(state.isFile()){
arr.push(current);
}
else {
const dirs = await fse.readdirSync(root);
for(let dir of dirs){
let appendDir = root + '/' + dir
arr = await checkIfALLAreCopied(appendDir, dir, arr);
}
}
return arr
}
module.exports = {
copyAllJsonNotesToASeparateFolder,
moveAllNotesToFolders,
checkIfALLAreCopied
}