-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.js
More file actions
57 lines (47 loc) · 1.57 KB
/
Main.js
File metadata and controls
57 lines (47 loc) · 1.57 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
/**
* Created by osujin12 on 2017. 6. 5..
*/
const fs = require('fs');
var dataDir = '/Users/test/Downloads/torrent/';
var moveDir = '/Users/test/Downloads/data/';
var moveList = "";
var walkSync = function(dir, filelist) {
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.splice(0, 1);
files.forEach(function(file) {
if (fs.statSync(dir + '/' + file).isDirectory()) {
filelist = walkSync(dir + '/' + file, filelist);
}
else {
filelist.push(file);
}
});
return files;
};
fs.watch(dataDir, function(event, filename) {
var fileList = walkSync(moveDir,"");
if(filename != '.DS_Store'){
console.log(filename)
var words = filename.split(/[\s.]+/);
if(event == "rename" && moveList != filename ) {
fileList.forEach(function (element) {
words.some(function (p1) {
var index = element.indexOf(p1);
if(index > -1){
console.log(dataDir+filename, moveDir+element+"/"+filename);
fs.rename(dataDir+filename, moveDir+element+"/"+filename,
function (err) {
if (err) console.error(err);
console.log('renamed complete'); });
moveList = filename;
return true;
}
});
});
}
}
});
var fileList = walkSync(moveDir,"");
console.log(fileList)