-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeleteqsos.js
More file actions
128 lines (100 loc) · 2.91 KB
/
deleteqsos.js
File metadata and controls
128 lines (100 loc) · 2.91 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// deleteqsos - a utility to delete qso files less than MINSIZE bytes in size
// and to delete folders that have no qso files in them.
//
var fs = require('fs');
const homedir = require('os').homedir();
const config = require('./config');
const util = require('util');
const MINSIZE = 200; // can change this if 200 is too small or too large
function sleep(millis)
{
return new Promise(resolve => setTimeout(resolve, millis));
}
function getFiles(dir)
{
files = fs.readdirSync(dir, { withFileTypes: true })
.filter(dirent => dirent.isFile())
.map(dirent => dirent.name)
return files;
}
function getDirectories(dir)
{
dirs = fs.readdirSync(dir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
return dirs;
}
function getFileSize(filePath)
{
let stats = fs.statSync(filePath);
let fileSizeInBytes = stats.size;
return fileSizeInBytes;
}
function deleteFolderRecursive(path)
{
if( fs.existsSync(path) )
{
fs.readdirSync(path).forEach(function(file,index)
{
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory())
{ // recurse
deleteFolderRecursive(curPath);
}
else
{ // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
function deleteqsos()
{
let qsodatadir = config.qsodatadir;
if(qsodatadir == "") // if no directory in config.js then use the default one
qsodatadir = homedir + '/.js8assistant/qsodata/'; // this is the default data directory
console.log("QSO Data Folder: " + qsodatadir);
let dirs = getDirectories(qsodatadir);
dirs.forEach(dir =>
{
let dirPath = qsodatadir + dir;
//console.log(dirPath);
let files = getFiles(dirPath)
files.forEach(file =>
{
let filePath = dirPath + '/' + file;
//console.log(filePath);
let fileSize = getFileSize(filePath);
//console.log(fileSize);
if(fileSize < MINSIZE)
{
console.log("Delete this file: " + filePath);
fs.unlinkSync(filePath);
}
});
});
// Now delete the empty folders
dirs.forEach(dir =>
{
let dirPath = qsodatadir + dir;
//console.log(dirPath);
let files = getFiles(dirPath)
if(files.length == 0)
{
console.log("Delete this folder: " + dirPath);
//fs.rmdirSync(dirPath);
deleteFolderRecursive(dirPath); // sometimes the folder can have sub folders due to a bug in saving
}
});
}
let logPath = process.argv[2];
let ourLog = logPath.replace("main.log", "deleteqsos.log");
let log_file = fs.createWriteStream(ourLog, {flags : 'w'});
let log_stdout = process.stdout;
console.log = function(d)
{
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
deleteqsos();