-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursefiles.js
More file actions
303 lines (277 loc) · 8.75 KB
/
Copy pathrecursefiles.js
File metadata and controls
303 lines (277 loc) · 8.75 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
var EJSON = require('mongodb-extended-json')
var convertjson = require('convert-json')
const xlsTypes = { exceed: 'exceed', result: 'result', summary: 'summary', scheme: 'scheme', shortfall: 'shortfall', compliance: 'compliance', supply: 'supply', numberofsamples: 'numberofsamples', numberofchecksamples: 'numberofchecksamples', none: '' }
const nullstring = ''
const folderSep = '/'
/*
// var BSON = require('bson')
// var XLSX = require('xlsx')
// var alltojson = require('allto-json')
function undef (obj) {
if (typeof obj === 'undefined' || obj === null) {
// variable is undefined or null
return true
} else {
return false
}
}
function groupBy (items, propertyName) {
var result = []
$.each(items, function (index, item) {
if ($.inArray(item[propertyName], result) == -1) {
result.push(item[propertyName])
}
})
return result
}
function stripName (name) {
}
function showTable (filename) {
var CvTb = require('convert-table')
CvTb({
input: filename,
table: {
chars: {
'top': '═',
'top-mid': '╤',
'top-left': '╔',
'top-right': '╗',
'bottom': '═',
'bottom-mid': '╧',
'bottom-left': '╚',
'bottom-right': '╝',
'left': '║',
'left-mid': '╟',
'mid': '─',
'mid-mid': '┼',
'right': '║',
'right-mid': '╢',
'middle': '│'
}
}
}, function (table) {
table.show() // call show method to show up the table
})
}
*/
function parseX (workbook) {
// var sheet = workbook.Sheets[0];\
if (typeof (workbook) === 'undefined') {
console.error('undefined workbook')
return
}
var sheetnamelist = workbook.SheetNames
var _xmlreturn = {}
sheetnamelist.forEach(function (y) {
var worksheet = workbook.Sheets[y]
var headers = {}
var data = []
for (let z in worksheet) {
if (z[0] === '!') continue
// parse out the column, row, and value
var tt = 0
for (var i = 0; i < z.length; i++) {
if (!isNaN(z[i])) {
tt = i
break
}
};
var col = z.substring(0, tt)
var row = parseInt(z.substring(tt))
var value = worksheet[z].v
// store header names
if (row === 1 && value) {
headers[col] = value
continue
}
if (!data[row]) data[row] = {}
data[row][headers[col]] = value
}
// drop those first two rows which are empty
// data.shift();
// data.shift();
if (typeof (y) !== 'undefined') {
if (data.length > 0) {
_xmlreturn[y] = data
}
// console.log(data.length);
}
})
// console.log(_xmlreturn);
return _xmlreturn
}
/*
function loadXLSX (filename) {
var workbook = XLSX.readFile(filename)
var sheetnamelist = workbook.SheetNames
// var _xmlreturn = {}
sheetnamelist.forEach(function (y) {
var worksheet = workbook.Sheets[y]
var headers = {}
var data = []
// let z = worksheet[0];
for (let z in worksheet) {
if (z[0] === '!') continue
// parse out the column, row, and value
var tt = 0
for (var i = 0; i < z.length; i++) {
if (!isNaN(z[i])) {
tt = i
break
}
};
var col = z.substring(0, tt)
var row = parseInt(z.substring(tt))
var value = worksheet[z].v
// store header names
if (row === 1 && value) {
headers[col] = value
continue
}
if (!data[row]) data[row] = {}
data[row][headers[col]] = value
}
// drop those first two rows which are empty
data.shift()
data.shift()
// sheet_name_list.y=data;
// console.log(data);
return data
})
// return $_;
return sheetnamelist.worksheet[0]
}
*/
// List all files in a directory in Node.js recursively in a synchronous fashion
var walkSync = function (dir, filelist) {
var path = require('path')
var fs = require('fs')
var files = fs.readdirSync(dir)
filelist = filelist || {}
files.forEach(function (file) {
if (dir === 'node_modules') {
} else if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = walkSync(path.join(dir, file), filelist)
} else {
// Array push
// filelist.push(path.join(dir, file));
// JSON elem
// "List of Exceedances"
// Summary
// "Monitoring Results"
// "Scheme Details" 1 miss mayo! Scheme fixes
// "Monitoring Shortfall" 2 miss
var $ = path.join(dir, file)
let posSlash = ($.indexOf(folderSep))
let posSp = ($.indexOf(' '))
let pos2ndSlash = (posSlash > 0) ? ($.substring(posSlash + 1, $.length - posSlash).indexOf(folderSep)) : -1
let ext = path.extname($)
// load if ext matches
// let county = filename.
// console.log ($);
// console.log ($.substring(posSlash+1, $.length-posSlash));
// console.log (pos2ndSlash);
// console.log (posSp);
// return {};
let props = {}
props['type'] = nullstring
props['year'] = (posSlash > 0) ? ($.substring(0, posSlash)) : nullstring
props['county'] = ((pos2ndSlash > 0) ? $.substring(posSlash + 1, posSlash + 1 + pos2ndSlash) : (posSp > 0) ? ($.substring(posSlash + 1, posSp)) : nullstring)
// props['1\\']=posSlash;
// props['2\\']=pos2ndSlash;
// props['1 ']=posSp;
// Dun - dun laoighaire
// DLR
// City vs County
// \\ contained take the left
// 2013
// Data_2013
// CSV
// Fingal
// Bray
// Drogheda
// Clonmel
// South - South Dublin
// North - North Tipperary
switch (true) {
case ($.indexOf('Exceedance') > 0 || $.indexOf('Exceedence') > 0):
props['type'] += xlsTypes.exceed
break
case ($.indexOf('Monitoring Results') > 0):
props['type'] += xlsTypes.result
break
case ($.indexOf('Summary') > 0):
props['type'] += xlsTypes.summary
break
case ($.indexOf('Shortfall') > 0):
props['type'] += xlsTypes.shortfall
break
case ($.indexOf('Scheme') > 0):
props['type'] += xlsTypes.scheme
break
case ($.indexOf('Compliance') > 0):
props['type'] += xlsTypes.compliance
break
case ($.indexOf('Supply') > 0) || ($.indexOf('Supplies') > 0):
props['type'] += xlsTypes.supply
break
case ($.indexOf('No. Of Samples') > 0) || ($.indexOf('No of Samples') > 0) || ($.indexOf('No. of Samples') > 0):
props['type'] += xlsTypes.numberofsamples
break
case ($.indexOf('NOOFCH') > 0):
props['type'] += xlsTypes.numberofchecksamples
break
default:
props['type'] += xlsTypes.none
}
if (props['type'] === xlsTypes.result) {
if (ext === '.xlsx') {
// console.log($);
// if(props["county"]=="Wicklow" && props["type"]==xlsTypes.exceed){
convertjson.xlsx(($), function (error, result) {
if (error) {
console.log(error)
}
props.xls = parseX(result)
})
// console.log(props.xls)
// })
// props.xls = showTable($);
// } else {
// props.xls ="{}";
// }
// console.log(props.xls);
}
if (ext === '.csv') {
convertjson.csv($, function (err, result) {
if (err) {
console.log(err)
}
// props.xls = parseX( result);
props.xls = result
// console.log(result);
})
}
if (ext === '.xls') {
convertjson.xls(($), function (error, result) {
if (error) {
console.log(error)
}
props.xls = parseX(result)
// props.xls = result;
// console.log(result)
})
}
}
filelist[$] = props
}
})
return filelist
}
let _flist = {}
walkSync('./2015', _flist)
console.log('[')
console.log(EJSON.stringify(_flist))
console.log(']')
// console.log( _flist);
// console.log(_flist.length)