Last active
December 30, 2015 07:29
-
-
Save forabi/7795945 to your computer and use it in GitHub Desktop.
This file contains 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 fs = require('fs'); | |
var path = require('path'); | |
fs.readdir(process.cwd(), function(err, files){ | |
var newNames = {}; | |
files.forEach(function(file){ | |
console.log(file); | |
if (file.match(/.+.txt/i)){ | |
console.log('Found text file', file); | |
fs.readFile(path.join(process.cwd(), file), 'utf8', function(err, data){ | |
newNames[file] = []; | |
data.split(/\r\n/g).forEach(function(line){ | |
console.log('Testing line in', file, ':', line); | |
var matches = line.match(/^\s*(\d+)\s*-\s*(.+)/i); | |
if (matches){ | |
var num = matches[1]; | |
console.log('Found mumber', num); | |
var obj = {}; | |
line = num + ' - ' + matches[2].trim(); | |
obj[num] = line; | |
fs.rename(path.join(process.cwd(), num + '.pdf'), path.join(process.cwd(), line.trim() + '.pdf'), function(err){ | |
if (err) throw err; | |
}) | |
newNames[file].push(obj); | |
console.log(obj); | |
} | |
}); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment