This was inspired by, and is based on a gist by Scriptex, index.js.
I have modified it to suit my purpose.
I have been downloading books from Zlibrary and the downloaded files have a naming format:
The Key things i want from this file name string are just the file name and authors(s) And i want to rename each file to:
- Use
filePattern
to filter for files that need to be renamed - Use
authorPattern
to extract Author name(s), and assign to the variableauthor
and remove underscores - Use
bookNamePattern
to extract book name and assign to the variablebook
and remove underscores - Combine
book
andauthor
and assign tonewName
. - Format:
book - @author.extension
- Create a new folder named
Files
in the root directory and place the files to be renamed in it. - In the index file, create your pattern (with a rename method)
- There are two ways to use the pattern:
- First call
RenameFiles.usePattern(patternClass)
. ThenRenameFiles.exec()
- Second,
RenameFiles.exec({ usePattern: patternClass })
NB:RenameFiles.exec()
returns a Promise:
RenameFiles.exec({ usePattern: patternClass }).then( console.log ).catch( console.error );
- First call
- Then use either
npm start
,node index
ornode .
to run.