Skip to content

Instantly share code, notes, and snippets.

@ducngtuan
Created May 4, 2018 22:14
Show Gist options
  • Save ducngtuan/c9c79a3fc259f8e6f785ffced6fe2490 to your computer and use it in GitHub Desktop.
Save ducngtuan/c9c79a3fc259f8e6f785ffced6fe2490 to your computer and use it in GitHub Desktop.
Script to rename files based on regex pattern in javascript
const fs = require('fs'),
path = require('path'),
dir = '~/Downloads',
match = /.*_(\w+\.part\d+.rar)/ // change this to whatever pattern needed
files = fs.readdirSync(dir)
files.filter(f => f.match(match)).forEach(f => {
const fpath = path.join(dir, f),
newfpath = path.join(dir, f.replace(match, (m, p1) => p1))
fs.renameSync(fpath, newfpath)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment