Skip to content

Commit

Permalink
File indicies per file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hotmann committed Jun 23, 2017
1 parent 4aaac56 commit 6aeabf9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ When you specify a RegEx pattern with the -r option, the regular expression will
```
##### *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```.*

1. 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.
1. Rename all files the same and an index number will be appended. The index will be prepended with 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. Each file extension in a rename operation will have its own independent index.
```sh
rename *.log test
Expand Down
30 changes: 25 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,31 @@ module.exports = {
thecommand: function(args) {
let newFileName = path.parse(_.last(args._));
let files = _.dropRight(args._);
if (globby.hasMagic(files)) {
if (globby.hasMagic(files)) { // glob file match on platforms that don't natively support it (Windows)
files = globby.sync(files);
}
let operations = [];
let fileIndex = 1;

// BUILD FILE INDICIES by file extension
let fileIndex = {};
if (newFileName.ext) {
fileIndex[newFileName.ext] = {
index: 1,
total: files.length
};
} else {
_.forEach(files, function(value) {
let fileObj = path.parse(path.resolve(value));
if (fileIndex[fileObj.ext]) {
fileIndex[fileObj.ext].total += 1;
} else {
fileIndex[fileObj.ext] = {
index: 1,
total: 1
};
}
});
}

// FOR EACH FILE
_.forEach(files, function(value) {
Expand All @@ -35,8 +55,8 @@ module.exports = {
let fileObj = path.parse(fullPath);
fileObj.newName = newFileName.name;
fileObj.newNameExt = (newFileName.ext ? newFileName.ext : fileObj.ext);
fileObj.index = fileIndex;
fileObj.totalFiles = files.length;
fileObj.index = fileIndex[fileObj.newNameExt].index;
fileObj.totalFiles = fileIndex[fileObj.newNameExt].total;

// REGEX match and group replacement
if (args.r) {
Expand Down Expand Up @@ -112,7 +132,7 @@ module.exports = {
operations.push({text: operationText, original: originalFileName, output: outputFileName});
}
}
fileIndex += 1;
fileIndex[fileObj.newNameExt].index += 1;
});

// PRINT simulated or verbose text
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.4.0",
"version": "3.0.0",
"description": "A command line utility for renaming files",
"main": "index.js",
"preferGlobal": true,
Expand Down

0 comments on commit 6aeabf9

Please sign in to comment.