Skip to content

Instantly share code, notes, and snippets.

@khaled0fares
Created April 29, 2017 22:59
Show Gist options
  • Save khaled0fares/634a0ab22fca123f72670567ec5575ec to your computer and use it in GitHub Desktop.
Save khaled0fares/634a0ab22fca123f72670567ec5575ec to your computer and use it in GitHub Desktop.
changing files extensions from txt to vim included in a directory
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