Created
April 29, 2017 22:59
-
-
Save khaled0fares/634a0ab22fca123f72670567ec5575ec to your computer and use it in GitHub Desktop.
changing files extensions from txt to vim included in a directory
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
const fs = require('fs') | |
const txtFetcherInDir = (dirname, callback)=>{ | |
fs.readdir(dirname, (err, files) => { | |
if (err) throw err; | |
const endWithtxt= files.filter((file)=>{ | |
const lastDot = file.lastIndexOf('.') + 1 | |
return file.substring(lastDot) === 'txt' | |
}) | |
callback(endWithtxt) | |
}); | |
} | |
const txtCutter = (text)=>{return text.substring(0, text.lastIndexOf('.'))} | |
txtFetcherInDir('.', (txtFiles)=> { | |
if(txtFiles.length == 0) return console.log("There are no files end with txt in this directory") | |
txtFiles.forEach((txtFormat)=>{ | |
const vimFormat = txtCutter(txtFormat) | |
fs.rename(txtFormat,vimFormat,(err)=>{ | |
if (err) throw err; | |
console.log(vimFormat) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment