Skip to content

Instantly share code, notes, and snippets.

@forabi
Last active December 30, 2015 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forabi/7795945 to your computer and use it in GitHub Desktop.
Save forabi/7795945 to your computer and use it in GitHub Desktop.
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