Home

Awesome

add-filename-increment Donate NPM version NPM monthly downloads NPM total downloads Build Status

When copying or moving files, it's common for operating systems to automatically add an increment or 'copy' to duplicate file names. This does that for Node.js applications, with automatic platform detection and support for Linux, MacOs, and Windows conventions.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm (requires Node.js >=8):

$ npm install --save add-filename-increment

What does this do?

When copying files, it's common for operating systems to append a numerical increment or the word 'copy' to a file name to prevent the existing file from being overwritten.

This library allows you to do the same thing in your Node.js application, using the correct conventions for the most commonly used operating systems.

Usage

All methods automatically detect the platform to use, unless platform is defined on the options.

const increment = require('add-filename-increment');

API

increment

The main export is a function that adds a trailing increment to the stem (basename without extension) of the given file path or object.

Params

Example

console.log(increment('foo/bar.txt', { platform: 'darwin' }));
//=> foo/bar copy.txt
console.log(increment('foo/bar.txt', { platform: 'linux' }));
//=> foo/bar (copy).txt
console.log(increment('foo/bar.txt', { platform: 'win32' }));
//=> foo/bar (2).txt

.path

Add a trailing increment to the given filepath.

Params

Example

console.log(increment.path('foo/bar.txt', { platform: 'darwin' }));
//=> foo/bar copy.txt
console.log(increment.path('foo/bar.txt', { platform: 'linux' }));
//=> foo/bar (copy).txt
console.log(increment.path('foo/bar.txt', { platform: 'win32' }));
//=> foo/bar (2).txt

.file

Add a trailing increment to the file.base of the given file object.

Params

Example

console.log(increment.file({ path: 'foo/bar.txt' }, { platform: 'darwin' }));
//=> { path: 'foo/bar copy.txt', base: 'bar copy.txt' }
console.log(increment.file({ path: 'foo/bar.txt' }, { platform: 'linux' }));
//=> { path: 'foo/bar (copy).txt', base: 'bar (copy).txt' }
console.log(increment.file({ path: 'foo/bar.txt' }, { platform: 'win32' }));
//=> { path: 'foo/bar (2).txt', base: 'bar (2).txt' }

.ordinal

Returns an ordinal-suffix for the given number. This is used when creating increments for files on Linux.

Params

Example

const { ordinal } = require('add-filename-increment');
console.log(ordinal(1)); //=> 'st'
console.log(ordinal(2)); //=> 'nd'
console.log(ordinal(3)); //=> 'rd'
console.log(ordinal(110)); //=> 'th'

.toOrdinal

Returns an ordinal for the given number.

Params

Example

const { toOrdinal } = require('add-filename-increment');
console.log(toOrdinal(1)); //=> '1st'
console.log(toOrdinal(2)); //=> '2nd'
console.log(toOrdinal(3)); //=> '3rd'
console.log(toOrdinal(110)); //=> '110th'

Options

options.fs

Description: Check the file system, and automatically increment the file based on existing files. Thus, if the file name is foo.txt, and foo (2).txt already exists, the file will automatically be renamed to foo (3).txt.

Also uses the correct conventions for Linux, Windows (win32), and MacOS (darwin).

Type: boolean

Default: undefined

options.increment

Description: Custom function to handling incrementing a file name. This is mostly useful when options.fs is also defined, since this function will only be called if a file name needs to be incremented, allowing you to control how incrementing is done.

Type: function

Default: undefined

options.platform

Description: Specify the platform conventions to use.

Type: string

Default: Uses process.platform. Valid values are linux, win32, and darwin.

Operating Systems

Supported Operating Systems

Currently Windows, Darwin (MacOS), and Linux are supported. This library attempts to automatically use the correct conventions for each operating system. Please create an issue if you ecounter a bug.

If you use an operating system with different conventions, and you would like for this library to add support, please create an issue with a detailed description of those conventions, or feel free to do a pull request.

Linux

When a file is copied or moved, and the destination file path already exists, Linux uses the following conventions for incrementing the file name.

Source pathDestination pathTypeDirectory<sup>1</sup>
foo.txtfoo (copy).txt, foo (another copy).txt, foo (3rd copy).txt, ...fileSame directory as source
foofoo (copy), foo (another copy), foo (3rd copy), ...directorySame directory as source

<small><sup>1</sup> On Linux, when a file or folder is copied or moved to a different directory and another file or folder with the same name exists in that directory, you are prompted to choose a new name for the file or folder, or to cancel or skip the operation. </small>

MacOS

When a file is copied or moved, and the destination file path already exists, MacOS uses the following conventions for incrementing the file name.

Source pathDestination pathTypeDirectory<sup>1</sup>
foo.txtfoo copy.txt, foo copy 2.txt, ...fileSame directory as source
foo.txtfoo 2.txt, foo 3.txt, ...fileDifferent directory than source
foofoo copy, foo copy 2, ...directorySame directory as source

<small><sup>1</sup> MacOS uses different conventions for incrementing file names when the source file is copied, moved or renamed to a different directory, versus when the file is copied into the same directory. </small>

Windows

When a file is copied or moved, and the destination file path already exists, Windows uses the following conventions for incrementing the file name.

Source pathDestination pathTypeDirectory<sup>1</sup>
foo.txtfoo - Copy.txtfileSame directory as source
foo.txtfoo (2).txtfileDifferent directory than source
foo (2).txtfoo (3).txtfileDifferent directory than source
foofoo - CopydirectorySame directory as source
foo - Copyfoo - Copy (2)directorySame directory as source

<small><sup>1</sup> Windows uses different conventions for incrementing file names when the source file is copied, moved or renamed to a different directory, versus when the file is copied into the same directory. Also, when a folder is copied to a new directory, and the new directory already has a folder with the same name, Windows just merges the folders automatically. </small>

About

<details> <summary><strong>Contributing</strong></summary>

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for advice on opening issues, pull requests, and coding standards.

</details> <details> <summary><strong>Running Tests</strong></summary>

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
</details> <details> <summary><strong>Building docs</strong></summary>

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb
</details>

Related projects

You might also be interested in these projects:

Author

Jon Schlinkert

License

Copyright © 2019, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on September 04, 2019.