Skip to content

Instantly share code, notes, and snippets.

@marcelosomers
Last active September 7, 2016 14:17
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 marcelosomers/cb137346b1caa79b8f1ce1dd6c707b0a to your computer and use it in GitHub Desktop.
Save marcelosomers/cb137346b1caa79b8f1ce1dd6c707b0a to your computer and use it in GitHub Desktop.
Get variables from file name and rename files
var fs = require('fs')
var filepath = require('filepath')
fs.readdir('./logos', function(err, files) {
var path = filepath.newPath()
files.forEach(function(file) {
// remove the extension
var splitFile = file.split('.')
// remove the extension and save to a variable
var extension = splitFile.pop()
var separator = "_"
var splitFileName = splitFile[0].split(separator)
if (splitFileName.length === 3) {
// get the three elements
var division = splitFileName[0]
var type = splitFileName[1]
var state = splitFileName[2]
var newFileName = `${state}_${division}_${type}.${extension}`
fs.rename(`${path}/logos/${file}`, `${path}/sorted/${newFileName}`, function(err) {
if (err) console.log(`ERROR: ${err}`)
})
} else {
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment