Last active
September 7, 2016 14:17
-
-
Save marcelosomers/cb137346b1caa79b8f1ce1dd6c707b0a to your computer and use it in GitHub Desktop.
Get variables from file name and rename files
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 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