-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortbydate.js
82 lines (61 loc) · 2.54 KB
/
sortbydate.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
console.log('Starting...\n');
var fs = require("fs");
var path = require('path');
var recursive = require('recursive-readdir');
var moment = require('moment');
moment.locale('de');
var mkdirp = require('mkdirp');
var mv = require('mv');
var myArgs = process.argv.slice(2);
//console.log(myArgs);
var directory = path.normalize(myArgs[0]);
console.log('Ausgewählt: ' + directory + '\n');
fs.stat(directory, function(err, stats) {
if (err) return console.log('Kein gültiger Pfad!');
if (stats.isDirectory()) {
console.log('Pfad ist gültig!');
sort();
} else {
console.log('Pfad ist kein Verzeichnis!');
}
});
var progress = function(number1, number2) {
var percent = number1 * 100 / number2;
//return percent.toFixed(2) + "% (" + number1 + '/' + number2 + ') ';
return parseInt(percent) + "% (" + number1 + '/' + number2 + ') ';
};
var sort = function() {
console.log('Inhalt wird gelesen...');
var directoryName = path.basename(directory);
var parentDir = path.dirname(directory);
console.log('parentDir=' + parentDir + ' && directoryName=' + directoryName);
var outputDir = path.join(parentDir, directoryName + '-sorted');
console.log('outputDir=' + outputDir + '\n\n');
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
recursive(directory, function(err, files) {
if (err) return console.log('Es ist ein Fehler aufgetreten!');
//console.log(files);
var length = files.length;
files.forEach(function(file, index) {
console.log(progress(index + 1, length) + file);
// http://www.unixtutorial.org/2008/04/atime-ctime-mtime-in-unix-filesystems/
var stats = fs.statSync(file);
if (stats.isFile()) {
var date = stats.mtime;
//console.log(date);
var destination = moment(date).format('YYYY/MM_MMMM/DD');
var destinationPath = path.join(outputDir, destination);
var fileName = path.basename(file);
var destinationFile = path.join(destinationPath, fileName);
console.log('Moving "' + fileName + '" => ' + destinationPath);
//if (!fs.existsSync(destinationPath)) mkdirp.sync(destinationPath);
mv(file, destinationFile, {
mkdirp: true
}, function(err) {
if (err) console.log('Error moving: ' + file);
});
}
});
console.log('\n\noutputDir=' + outputDir + '\n');
});
};