Skip to content

Commit

Permalink
Allow multiple RegEx options, width and height from EXIF data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hotmann committed Apr 12, 2018
1 parent 1e82449 commit 30fdf14
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 70 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The new file name can contain any number of variables that will be replaced with

```{{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.
```{{r}}``` RegEx First: The first match of the RegEx pattern specified in -r "...".
```{{r}}``` RegEx: The match of the RegEx pattern(s) specified in -r "...". Parameters: the index of the regex match, default is 0.
```{{ra}}``` RegEx All: All matches of the RegEx pattern specified in -r "...". Parameters: separator character(s), default is none.
```{{rn}}``` RegEx Not: Everything except for the matches of the RegEx pattern specified in -r "...". Parameters: replacement character(s), default is none
```{{p}}``` Parent directory: The name of the parent directory. Parameters: upper, lower, camel, pascal, or none for unmodified.
Expand All @@ -51,6 +51,8 @@ The new file name can contain any number of variables that will be replaced with
```{{efnum}}``` Exif FNumber: Photo FNumber value.
```{{eex}}``` Exif Exposure Time: Photo exposure time value.
```{{ed}}``` Exif Date: The date/time photo was taken. Parameters: date format, default is yyyymmdd.
```{{eh}}``` Exif Height: The height in pixels of the photo
```{{ew}}``` Exif Width: The width in pixels of the photo

### 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. 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, an empty string will be returned. **DO NOT** include the forward slashes in your RegEx pattern.
Expand Down Expand Up @@ -89,13 +91,20 @@ When you specify a RegEx pattern with the -r option, the regular expression will
ExpenseReport - October 2015.pdf → 2015 - October Expense Report.pdf
```
1. 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.
1. 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 ```|``` which means 'or', and finally ```\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
```
1. Use multiple RegEx options with `{{rn}}` to filter out different parts of the input file name in order. RegEx and parameter explaination: first .2016. and all following characters are replaced due to the first RegEx rule `-r "\.\d{4}\..+"`, then we match just the year with the second RegEx rule for use later with `{{r|1}}`, and then all periods are replaced due to the third RegEx rule. Finally we add back the year inside parenthesis. Since JavaScript uses 0 as the first index of an array, 1 finds the second regex match which is just the year as specified by ` -r "\d{4}"`.
```sh
rename -r "\.\d{4}\..+" -r "\d{4}" -r "\." My.File.With.Periods.2016.more.info.txt "{{rn| }}({{r|1}})"
My.File.With.Periods.2016.more.info.txt → My File With Periods (2016).txt
```
1. Extract Exif data from jpg images.
```sh
Expand Down
37 changes: 31 additions & 6 deletions lib/replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ const replacements = {
}
},
'r': {
name: 'RegEx First',
description: 'The first match of the RegEx pattern specified in -r "..."',
name: 'RegEx',
description: 'The specified match of the RegEx pattern(s) specified in -r "..."',
parameters: {
description: 'the number of the regex match, default is 0',
default: '0'
},
unique: false,
function: function(fileObj) {
if (fileObj.regexMatches) {
return fileObj.regexMatches[0];
function: function(fileObj, arg) {
let matchNum = parseInt(arg);
if (fileObj.regexMatches && fileObj.regexMatches[matchNum]) {
return fileObj.regexMatches[matchNum];
} else {
return '';
}
Expand Down Expand Up @@ -85,7 +90,9 @@ const replacements = {
unique: false,
function: function(fileObj, args) {
args = (args ? args : '');
return fileObj.name.replace(fileObj.regexPattern, args);
let output = fileObj.name;
fileObj.regexPatterns.forEach((pattern) => { output = output.replace(pattern, args); });
return output;
}
},
'p': {
Expand Down Expand Up @@ -232,6 +239,24 @@ const replacements = {
let formattedDate = data.DateTime.split(/:|\s/)[1] + '/' + data.DateTime.split(/:|\s/)[2] + '/' + data.DateTime.split(/:|\s/)[0] + ' ' + data.DateTime.split(/:|\s/)[3] + ':' + data.DateTime.split(/:|\s/)[4] + ':' + data.DateTime.split(/:|\s/)[5];
return (typeof(data) === 'object' && data.DateTime ? dateFormat(formattedDate, args) : '');
}
},
'eh': {
name: 'Exif Height',
description: "The height in pixels of the photo",
unique: false,
function: function(fileObj) {
let data = getExifData(fileObj.dir + '/' + fileObj.base);
return (typeof(data) === 'object' && data.SubExif && data.SubExif.PixelYDimension ? data.SubExif.PixelYDimension : '');
}
},
'ew': {
name: 'Exif Width',
description: "The width in pixels of the photo",
unique: false,
function: function(fileObj) {
let data = getExifData(fileObj.dir + '/' + fileObj.base);
return (typeof(data) === 'object' && data.SubExif && data.SubExif.PixelXDimension ? data.SubExif.PixelXDimension : '');
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions lib/userReplacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@
newNameExt: '.ext',
index: '29',
regexMatches: ['match1', 'match2', ...],
regexPattern: /SomeRexEx/g,
regexPatterns: [/SomeRegEx/g],
totalFiles: '50'
}
Note: regexMatches and regexPattern are only present when the -r option is used, so be sure
Note: regexMatches and regexPatterns are only present when the -r option is used, so be sure
to check if they exist before proceeding.
You can then use these properties if desired in your functions. See the
Expand Down
83 changes: 50 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rename-cli",
"version": "5.0.1",
"version": "5.1.0",
"description": "A command line utility for renaming files",
"main": "rename.js",
"preferGlobal": true,
Expand All @@ -20,19 +20,19 @@
},
"dependencies": {
"blessed": "^0.1.81",
"chalk": "^2.3.0",
"chalk": "^2.3.2",
"cli-clear": "^1.0.4",
"clipboardy": "^1.2.2",
"clipboardy": "^1.2.3",
"dateformat": "^2.2.0",
"fs-extra": "^2.0.0",
"globby": "^6.1.0",
"inquirer": "^3.3.0",
"jpeg-exif": "^1.0.6",
"named-js-regexp": "^1.3.3",
"num2fraction": "^1.2.2",
"opn": "^5.1.0",
"opn": "^5.3.0",
"path-exists": "^3.0.0",
"prompt-sync": "^4.1.5",
"prompt-sync": "^4.1.6",
"remark": "^8.0.0",
"yargs": "^8.0.2"
},
Expand Down
Loading

0 comments on commit 30fdf14

Please sign in to comment.