Skip to content

How to use renamer programmatically

Lloyd Brookes edited this page Mar 10, 2021 · 8 revisions

First read the API docs.

Typical usage

Assuming your current folder looks like this:

pics
├── pic1.jpg
├── pic2.jpg

Running this script...

import Renamer from 'renamer/index.mjs'
const renamer = new Renamer()

renamer.on('replace-result', replaceResult => {
  console.log(replaceResult)
})

renamer.rename({
  files: [ 'pics/*' ],
  find: 'pic',
  replace: 'photo',
  dryRun: true
})

...will result in the following output.

$ node example.js
{ from: 'pics/pic1.jpg',
  to: 'pics/photo1.jpg',
  renamed: true }
{ from: 'pics/pic2.jpg',
  to: 'pics/photo2.jpg',
  renamed: true }
{ from: 'pics/pics',
  to: 'pics/photos',
  renamed: true }

Remove the dryRun: true flag to complete the renames on disk.