Last active
          January 4, 2016 08:18 
        
      - 
      
- 
        Save plasticut/8593969 to your computer and use it in GitHub Desktop. 
     Placing files into folders on the date the file was created (Размещение файлов по папкам относительно даты создания файла)
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var path = require('path'); | |
| var fs = require('fs'); | |
| var noColor = process.argv.indexOf("--nocolor") > -1; | |
| var testMode = process.argv.indexOf("--test") > -1; | |
| function two(num) { | |
| return ("0" + num).slice(-2); | |
| } | |
| function formatDate(date) { | |
| return "" + date.getFullYear() + "-" + two(date.getMonth()) + "-" + two(date.getDate()); | |
| } | |
| function formatSize(size) { | |
| var s = ""; | |
| while (size > 0) { | |
| s = "" + size%1000 + " " + s; | |
| size = Math.floor(size/1000); | |
| } | |
| return s; | |
| } | |
| function quoted(text){ | |
| return "\"" + text + "\""; | |
| } | |
| var styles = { | |
| 'green' : ['\x1B[32m', '\x1B[39m'], | |
| 'red' : ['\x1B[31m', '\x1B[39m'] | |
| }; | |
| function color(colorName, text) { | |
| if (noColor) { return text; } | |
| return styles[colorName][0] + text + styles[colorName][1]; | |
| } | |
| function action(srcDir, dstDir, next) { | |
| if (!srcDir) { return next("Source directory not specified"); } | |
| if (!dstDir) { return next("Source directory not specified"); } | |
| if (!fs.existsSync(srcDir)) { return next("Source directory not found"); } | |
| if (!fs.existsSync(dstDir)) { return next("Destination directory not found"); } | |
| if (testMode) { console.log("Testing..."); } | |
| console.log("Source:", quoted(srcDir)); | |
| console.log("Destination:", quoted(dstDir)); | |
| var srcFiles = []; | |
| var srcStat, dstStat, srcFilePath, dstFilePath; | |
| var dir, dirs = {}; | |
| var count = 0; | |
| fs.readdirSync(srcDir).forEach(function(file) { | |
| srcFilePath = path.join(srcDir, file); | |
| srcStat = fs.statSync(srcFilePath); | |
| if (srcStat.isFile()) { | |
| dir = path.join(dstDir, formatDate(srcStat.mtime)); | |
| srcFiles.push({ | |
| srcStat: srcStat, | |
| srcFilePath: srcFilePath, | |
| dir: dir, | |
| dstFilePath: path.join(dir, file) | |
| }); | |
| } | |
| }); | |
| console.log("Found", color("green", srcFiles.length), "files"); | |
| srcFiles.forEach(function(info) { | |
| srcStat = info.srcStat; | |
| srcFilePath = info.srcFilePath; | |
| dir = info.dir; | |
| dstFilePath = info.dstFilePath; | |
| if (!testMode) { | |
| if (!dirs[dir]) { | |
| try { | |
| fs.mkdirSync(dir); | |
| } catch(e) { | |
| console.log("Directory", color("red", quoted(dir)), "already exists"); | |
| } | |
| dirs[dir] = true; | |
| } | |
| } | |
| if (fs.existsSync(dstFilePath)) { | |
| dstStat = fs.statSync(dstFilePath); | |
| console.log("File", color("red", quoted(dstFilePath)), "already exists"); | |
| console.log("\tOld:\t\t\tNew:"); | |
| console.log("\tsize: ", color("green", formatSize(srcStat.size)), "\tsize: ", color("green", formatSize(dstStat.size))); | |
| console.log("\taccess:", color("green", formatDate(srcStat.atime)), "\taccess:", color("green", formatDate(dstStat.atime))); | |
| console.log("\tmodify:", color("green", formatDate(srcStat.mtime)), "\tmodify:", color("green", formatDate(dstStat.mtime))); | |
| console.log("\tcreate:", color("green", formatDate(srcStat.ctime)), "\tcreate:", color("green", formatDate(dstStat.ctime))); | |
| return; | |
| } | |
| if (!testMode) { fs.renameSync(srcFilePath, dstFilePath); } | |
| console.log("File", color("red", quoted(dstFilePath)), "moved"); | |
| count++; | |
| }); | |
| next(null, count); | |
| } | |
| action(process.argv.filter(function(item) { return item[0] !== "-"; })[2], process.argv.filter(function(item) { return item[0] !== "-"; })[3], function(err, count) { | |
| if (err) { return console.error(color("red", err)); } | |
| console.log("Moved", color("green", count), "files"); | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment