-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyrandom.js
154 lines (139 loc) · 4.18 KB
/
copyrandom.js
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
var _ = require('underscore')
var fs = require('fs-extra')
var path = require('path')
var glob = require('glob')
var util = require('util')
var async = require('async')
// ===================================================================
// Global variables
// ===================================================================
var rawFiles, organizedFiles = {}
// ===================================================================
// Aux functions
// ===================================================================
var generateRandomNumbers = function(max, limit) {
var arr = []
while(arr.length < limit) {
var randomnumber = Math.round(Math.random() * max)
var found = false
for(var i = 0; i < arr.length; i++) {
if(arr[i] == randomnumber) {
found = true
break
}
}
if(!found) {
arr[arr.length] = randomnumber
}
}
return arr
}
// ===================================================================
// Functionality
// ===================================================================
var copyRandom = function(origen, destino, limit) {
origen = path.resolve(__dirname, origen)
destino = path.resolve(__dirname, destino)
limit = Number(limit)
async.waterfall([
// Check if the origin folder exists
function checkIfOriginExists (cb) {
fs.exists(origen, function (exists) {
return (exists ? cb() : cb('No existe el directorio ' + origen))
})
},
// Check if the destiny folder exists
function checkIfDestinyExists (cb) {
fs.exists(destino, function (exists) {
return (exists ? cb() : cb('No existe el directorio ' + destino))
})
},
// Analyze folder content
function diveFolderContent (cb) {
glob(path.join(origen, '**', '*.mp3'), {}, function (error, results) {
if (error) {
return cb(error)
}
if (!results || !results.length) {
return cb('No se han encontrado archivos en ' + origen)
}
else {
cb(null, results)
}
})
},
// Pick random files
function pickRandomFiles (files, cb) {
var randomFiles = []
var desiredFiles = Math.min(files.length, limit)
var randomNumbers = generateRandomNumbers(files.length, desiredFiles)
for(var i = 0; i < desiredFiles; i++) {
randomFiles.push(files[randomNumbers[i]])
}
cb(null, randomFiles)
},
// Fix file paths
function fixFilePaths (files, cb) {
for(var i in files) {
files[i] = files[i].replace(/\//gi, path.sep)
}
cb(null, files)
},
// Copy results
function copyFiles (files, cb) {
var count = 0
var total = files.length
async.each(files, function(file, cb1) {
fs.copy(file, path.join(destino, path.basename(file)), function(error) {
if(error) {
return cb1(error)
}
// Print progress
var msg = util.format('Copiados %d de %d archivos (%d%%)', ++count, total, Math.round(count / total * 10000) / 100)
process.stdout.write(msg + ' \r')
cb1()
})
}, function(error) {
if(error) {
return cb(error)
}
cb(null, files)
})
}
], function (error, results) {
if (error) {
return console.log(error)
}
console.log('')
console.log('Se han copiado %d archivos aleatorios a %s', limit, destino)
console.log('')
for(var i in results) {
console.log('\t► %s', results[i])
}
console.log('')
})
}
// ===================================================================
// Usage
// ===================================================================
var usage = function () {
console.log('Script para copiar archivos aleatorios de un directorio y sus subdirectorios.')
console.log('')
console.log('PARÁMETROS (entre comillas dobles):')
console.log('\t1\tPath relativo al directorio origen.')
console.log('\t1\tPath relativo al directorio destino.')
console.log('\t1\tLímite numérico')
console.log('')
console.log('EJEMPLOS:')
console.log('node copyrandom ../../Music/Electrónica/House F:\\ 10')
}
// ===================================================================
// Main program
// ===================================================================
var main = function (args) {
if(args.length < 5 || args[2].toLowerCase() == 'usage') {
return usage()
}
return copyRandom(args[2], args[3], args[4])
}
main(process.argv)