Skip to content

How to use renamer programmatically

Lloyd Brookes edited this page May 3, 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'

const renamer = new Renamer()
const options = {
  files: ['pics/*'],
  find: 'pic',
  replace: 'photo',
  dryRun: true
}
for await (const result of renamer.results(options)) {
  console.log(result)
}

...will result in the following output.

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

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