Get an unused filename by appending a number if it exists:
file.txt
→file (1).txt
Useful for safely writing, copying, moving files without overwriting existing files.
$ npm install unused-filename
.
├── rainbow (1).txt
├── rainbow.txt
└── unicorn.txt
const unusedFilename = require('unused-filename');
(async () => {
console.log(await unusedFilename('rainbow.txt'));
//=> 'rainbow (2).txt'
})();
Returns a Promise<string>
containing either the original filename
or the filename
appended with a number.
Returns a string
containing either the original filename
or the filename
appended with a number.
Type: string
The path to check for filename collision.
- filenamify - Convert a string to a valid safe filename
MIT © Sindre Sorhus