Youtube indirilen dosyaları taşıma


var fs = require('fs');
var mkdirp = require('mkdirp');
/**
 *  I have files like this: ['01 01 Course Overview-nuuCJ1CK2Xw.mp4','01 01 Course Overview-nuuCJ1CK2Xw.mp4','04 04 How to Split a Component to Render Differently in iOS and Android-gXQWQPXDRR0.mp4']
 *  First 2 digit is chapters second 2 digit is lesson numbers.
 *  I want to split files in respective folders.
 */

// Define source and destionation folders.
var source = '/home/xuma/Desktop/ReactNative/';
var destination = '/home/xuma/Desktop/output/';

var re = /(.*?)(\-(.*?))\.mp4/;
fs.readdir(source,function(err,items){
    items.forEach(function(item){
        var m;
        if ((m = re.exec(item)) !== null) {
            if (m.index === re.lastIndex) {
                re.lastIndex++;
            }
            var structure = m[1].split(' ');
            var folder = structure[0];
            mkdirp(destination+folder, function(err) {
               fs.writeFileSync(destination+folder+'/'+m[1].slice(3)+'.mp4', fs.readFileSync(source+item));
            });
        }
    });

});