Skip to content

Commit

Permalink
Added ability to use all regex matches in output file name
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hotmann committed Mar 26, 2017
1 parent 780f76e commit 0c8acdc
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The new file name does not need to contain a file extension. If you do not speci
```-h```, ```--help```: Show help

### Variables
The new file name can contain any number of variables that will be replaced with their value. Some variables can take parameters and will be indicated in their description. To pass a parameter to a variable, just use the variable name followed by a pipe and the parameter. The output file name must be surrounded by quotes when using parameters. See the first example below for how to use parameters.
The new file name can contain any number of variables that will be replaced with their value. Some variables can take parameters and will be indicated in their description. To pass a parameter to a variable, just use the variable name followed by a pipe and the parameter. **The output file name must be surrounded by quotes when using parameters.** See the first example below for how to use parameters.

```{{i}}``` Index: The index of the file when renaming multiple files. Parameters: starting index, default is 1.
```{{f}}``` File name: The original name of the file. Parameters: upper, lower, camel, pascal, or none for unmodified.
Expand All @@ -45,7 +45,7 @@ The new file name can contain any number of variables that will be replaced with
```{{ed}}``` Exif Date: The date/time photo was taken. Parameters: date format, default is yyyymmdd.

### RegEx
When you specify a RegEx pattern with the -r option, the regular expression will be run against the original file name and the first match will be used to replace {{r}} in the output file name. If the regular expression fails to match, and empty string will be returned. **DO NOT** include the forward slashes in your RegEx pattern.
When you specify a RegEx pattern with the -r option, the regular expression will be run against the original file name and the first match will be used to replace {{r}} in the output file name. You can also use {{ra}} in the output file name to keep all matches separated by a string you supply as an argument (or no argument to just append all matches together). If the regular expression fails to match, and empty string will be returned. **DO NOT** include the forward slashes in your RegEx pattern.

Groups:
You can write RegEx to capture one or more named groups and then use those groups in your output file name. The groups should be written like: ```(?<GroupName>regular expression here)```. If the RegEx groups do not return a match, the replacement variables in the output file name will be blank, so be sure to test with the -s option. See the third example below for how to use RegEx groups.
Expand All @@ -59,7 +59,7 @@ rename *.log "{{d|yyyymmdd}}{{f}}"
node.log → 20170303node.log
system.log → 20170303system.log
```
##### *Note: the default format for the date variable is yyyymmdd so in the above example you could just write ```rename *.log {{d}}{{f}}``` to achieve the same result.*
##### *Note: the default format for the date variable is yyyymmdd so in the above example you could just write ```rename *.log {{d}}{{f}}``` to achieve the same result. You can see default parameters for variables by typing ```rename -v```.*

Rename all files the same and an index will be appended. The index will be prepended the correct number of zeroes to keep file order the same. For example if you are renaming 150 files, the first index will be 001. You can change the starting index by adding the index variable with a parameter ```{{i|42}}``` If you don't want to include indexes use the ```-n``` option. You will be prompted for any file conflicts.

Expand All @@ -78,6 +78,13 @@ rename -r "- (?<month>[A-Za-z]+) (?<year>\d{4})" --noindex ExpenseReport*.pdf "{
ExpenseReport - October 2015.pdf → 2015 - October Expense Report.pdf
```

Use all RegEx matches in the output file name separated by a space. RegEx explaination: ```\w+``` captures a string of 1 or more word characters (A-Z, a-z, and _), ```(?=.+\d{4})``` is a forward lookahead for a number of 4 digits (this means it will only find words before the number), and then ```|``` or, ```\d{4}``` a number of 4 digits.

```sh
rename -r "\w+(?=.+\d{4})|\d{4}" My.File.With.Periods.2016.more.info.txt "{{ra| }}"
My.File.With.Periods.2016.more.info.txt → My File With Periods 2016.txt
```

Extract Exif data from jpg images.

```sh
Expand Down
1 change: 1 addition & 0 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const argv = require('yargs')
boolean: true,
describe: 'Undo previous rename operation'
}, 'r': {
alias: 'regex',
describe: 'See RegEx section of online help for more information',
type: 'string'
}, 'f': {
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ module.exports = {
if (args.r) {
let pattern;
try {
pattern = new RegExp(args.r.replace(/\(\?\<\w+\>/g, '('));
pattern = new RegExp(args.r.replace(/\(\?\<\w+\>/g, '('), 'g');
} catch (err) {
console.log(err.message);
process.exit(1);
}
fileObj.regexMatch = pattern.exec(fileObj.name);
fileObj.regexMatches = fileObj.name.match(pattern);

let groupNames = args.r.match(/\<[A-Za-z]+\>/g);
if (groupNames !== null) {
Expand All @@ -72,7 +72,7 @@ module.exports = {
}

// REPLACEMENT VARIABLES replace the replacement strings with their value
let repSearch = /\{{2}([\w]+?)\}{2}|\{{2}([\w]+?)\|(.+?)\}{2}/;
let repSearch = /\{{2}([\w]+?)\}{2}|\{{2}([\w]+?)\|(.*?)\}{2}/;
let repResult = repSearch.exec(fileObj.newName);
while (repResult !== null) {
let repVar = repResult[1] || repResult[2];
Expand Down
25 changes: 21 additions & 4 deletions lib/replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,29 @@ const replacements = {
}
},
'r': {
name: 'RegEx',
description: 'The match of the RegEx pattern specified in -r "..."',
name: 'RegEx First',
description: 'The first match of the RegEx pattern specified in -r "..."',
unique: false,
function: function(fileObj) {
if (fileObj.regexMatch) {
return fileObj.regexMatch[0];
if (fileObj.regexMatches) {
return fileObj.regexMatches[0];
} else {
return '';
}
}
},
'ra': {
name: 'RegEx All',
description: 'All matches of the RegEx pattern specified in -r "..."',
parameters: {
description: 'separator character(s), default is none',
default: ''
},
unique: false,
function: function(fileObj, args) {
if (fileObj.regexMatches) {
args = (args ? args : '');
return fileObj.regexMatches.join(args);
} else {
return '';
}
Expand Down
4 changes: 4 additions & 0 deletions lib/userReplacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@
newName: 'new-file-name',
newNameExt: '.ext',
index: '29',
regexMatches: ['match1', 'match2', ...],
totalFiles: '50'
}
Note: regexMatches is only present when the -r option is used, so be sure
to check if it exists before proceeding.
You can then use these properties if desired in your functions. See the
default variable replacements here:
https://github.com/jhotmann/node-rename-cli/blob/master/replacements.js
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rename-cli",
"version": "2.0.0",
"version": "2.1.0",
"description": "A command line utility for renaming files",
"main": "index.js",
"preferGlobal": true,
Expand Down

0 comments on commit 0c8acdc

Please sign in to comment.