npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

filename-incrementer

v2.0.2

Published

Increment filenames

Readme

Filename Incrementer

Tools to Increment filenames.

Usage

Install with npm:

npm install filename-incrementer

Example:

var incr = require('filename-incrementer');

// Get filename with incremented value.
incr.getNewName('path/filename-###.ext')
  .then(name => console.log(name));

// logs: 'filename-3.ext', assuming there was already a filename-2.ext in that directory

Methods

Almost all methods accept the two same parameters:

  • {string} path - Path to files, with '###' (or pattern described in incrPattern) where the incrementer is set, e.g. '/path/filename-###.ext'.
  • {string} [incrPattern] - If for one reason you want to use a different pattern than '###' to match the incrementer's place in the filename, you provide it here.

Exampels below assume the following file structure:

directory
   |- example-3.ext
   |- example-4.ext
   |- file-3.ext
   |- file-4.ext
   |- test-1.ext
   |- test-2.ext
   |- test-3.ext

The first two methods will probably be the most usefull ones:

getNew

Get incremented value for files matching the pattern.

Signature:

getNew(path: string, incrPattern: string = '###'): Promise<number>

Examples:

incr.getNew('directory/file-###.ext')
  .then(value => console.log(value));

// logs: 5


incr.getNew('directory/test-@@.ext', '@@')
  .then(value => console.log(value));

// logs: 4

getNewName

Get filename with incremented value.

Signature:

getNewName(path: string, incrPattern: string = '###'): Promise<string>

Examples:

incr.getNewName('directory/file-###.ext')
  .then(name => console.log(name));

// logs: 'file-5.ext'


incr.getNewName('directory/test-@@.ext', '@@')
  .then(name => console.log(name));

// logs: 'test-4.ext'

getMax

Get max. incrementer value for files matching the pattern.

Signature:

getMax(path: string, incrPattern: string = '###'): Promise<number>

Examples:

incr.getMax('directory/file-###.ext')
  .then(value => console.log(value));

// logs: 4


incr.getMax('directory/test-@@.ext', '@@')
  .then(value => console.log(value));

// logs: 3

getMin

Get min. incrementer value for files matching the pattern.

Signature:

getMin(path: string, incrPattern: string = '###'): Promise<number>

Example:

incr.getMin('directory/example-###.ext')
  .then(value => console.log(value));

// logs: 3

getFiles

Get file paths matching the pattern.

Signature:

getFiles(path: string, incrPattern: string = '###'): Promise<string[]>

Example:

incr.getFiles('directory/example-###.ext')
  .then(files => console.log(files));

// logs: [
//   'directory/example-3.ext',
//   'directory/example-4.ext'
// ]

getFilesMap

Get map of paths matching the pattern and their incrementer value.

Signature:

getFilesMap(path: string, incrPattern: string = '###'): Promise<{[path: string]: number}>

Example:

incr.getFiles('directory/example-###.ext')
  .then(map => console.log(map));

// logs: {
//   'directory/example-3.ext': 3,
//   'directory/example-4.ext': 4
// }

getMatchPattern

Get glob or regexp matching pattern.

Signature:

getMatchPattern(type: 'glob'|'regexp', path: string, incrPattern: string = '###'): string

Examples:

let ptrn1 = incr.getMatchPattern('glob', 'directory/file-###.ext');

console.log(ptrn1);

// logs: 'directory/file-+([0-9]).ext'


let ptrn2 = incr.getMatchPattern('regexp', 'directory/test-@@.ext', '@@');

console.log(ptrn2);

// logs: '^directory/test\-([0-9]+)\.ext$'