Skip to content

Commit

Permalink
add flag --nr-width to fill leading 0s for {nr}
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Mar 19, 2018
1 parent b8f44ad commit 9cc1d83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@

#### Method 1: Download binaries

[brename v2.5.1](https://github.com/shenwei356/brename/releases/tag/v2.5.1)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/brename/v2.5.1/total.svg)](https://github.com/shenwei356/brename/releases/tag/v2.5.1)
[brename v2.5.2](https://github.com/shenwei356/brename/releases/tag/v2.5.2)
[![Github Releases (by Release)](https://img.shields.io/github/downloads/shenwei356/brename/v2.5.2/total.svg)](https://github.com/shenwei356/brename/releases/tag/v2.5.2)

***Tip: run `brename -V` to check update !!!***

OS |Arch |File, 中国镜像 |Download Count
:------|:---------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Linux |32-bit |[brename_linux_386.tar.gz](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_linux_386.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_linux_386.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_linux_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_linux_386.tar.gz)
Linux |**64-bit**|[**brename_linux_amd64.tar.gz**](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_linux_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_linux_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_linux_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_linux_amd64.tar.gz)
OS X |32-bit |[brename_darwin_386.tar.gz](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_darwin_386.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_darwin_386.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_darwin_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_darwin_386.tar.gz)
OS X |**64-bit**|[**brename_darwin_amd64.tar.gz**](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_darwin_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_darwin_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_darwin_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_darwin_amd64.tar.gz)
Windows|32-bit |[brename_windows_386.exe.tar.gz](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_windows_386.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_windows_386.exe.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_windows_386.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_windows_386.exe.tar.gz)
Windows|**64-bit**|[**brename_windows_amd64.exe.tar.gz**](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_windows_amd64.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_windows_amd64.exe.tar.gz)|[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_windows_amd64.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.1/brename_windows_amd64.exe.tar.gz)
Linux |32-bit |[brename_linux_386.tar.gz](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_linux_386.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_linux_386.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_linux_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_linux_386.tar.gz)
Linux |**64-bit**|[**brename_linux_amd64.tar.gz**](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_linux_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_linux_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_linux_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_linux_amd64.tar.gz)
OS X |32-bit |[brename_darwin_386.tar.gz](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_darwin_386.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_darwin_386.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_darwin_386.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_darwin_386.tar.gz)
OS X |**64-bit**|[**brename_darwin_amd64.tar.gz**](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_darwin_amd64.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_darwin_amd64.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_darwin_amd64.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_darwin_amd64.tar.gz)
Windows|32-bit |[brename_windows_386.exe.tar.gz](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_windows_386.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_windows_386.exe.tar.gz) |[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_windows_386.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_windows_386.exe.tar.gz)
Windows|**64-bit**|[**brename_windows_amd64.exe.tar.gz**](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_windows_amd64.exe.tar.gz),<br/> [中国镜像](http://app.shenwei.me/data/brename/brename_windows_amd64.exe.tar.gz)|[![Github Releases (by Asset)](https://img.shields.io/github/downloads/shenwei356/brename/latest/brename_windows_amd64.exe.tar.gz.svg?maxAge=3600)](https://github.com/shenwei356/brename/releases/download/v2.5.2/brename_windows_amd64.exe.tar.gz)


Just [download](https://github.com/shenwei356/brename/releases) compressed
Expand Down Expand Up @@ -95,7 +95,7 @@ And then:
```
brename -- a practical cross-platform command-line tool for safely batch renaming files/directories via regular expression
Version: 2.5.1
Version: 2.5.2
Author: Wei Shen <[email protected]>
Expand Down Expand Up @@ -154,13 +154,13 @@ Flags:
-k, --kv-file string tab-delimited key-value file for replacing key with value when using "{kv}" in -r (--replacement)
-l, --list only list paths that match pattern
-a, --list-abs list absolute path, using along with -l/--list
--nr-width int minimum width for {nr} in flag -r/--replacement. e.g., formating "1" to "001" by --nr-width 3 (default 1)
-p, --pattern string search pattern (regular expression)
-R, --recursive rename recursively
-r, --replacement string replacement. capture variables supported. e.g. $1 represents the first submatch. ATTENTION: for *nix OS, use SINGLE quote NOT double quotes or use the \ escape character. Ascending integer is also supported by "{nr}"
-n, --start-num int starting number when using {nr} in replacement (default 1)
-v, --verbose int verbose level (0 for all, 1 for warning and error, 2 for only error)
-V, --version print version information and check for update
```


Expand Down Expand Up @@ -316,6 +316,12 @@ Take a directory for example:
[INFO] checking: [ ok ] 'b.jpg' -> 'pic-2.jpg'
[INFO] 2 path(s) to be renamed

$ brename -f '.jpeg' -e -p '(.+)' -r 'pic-{nr}' --nr-width 3 -d
[INFO] checking: [ ok ] 'a.jpeg' -> 'pic-001.jpeg'
[INFO] checking: [ ok ] 'b.jpeg' -> 'pic-002.jpeg'
[INFO] 2 path(s) to be renamed


1. Replace submatch with corresponding value via key-value file (`-k/--kv-file`)

$ more kv.tsv
Expand Down Expand Up @@ -360,7 +366,7 @@ Take a directory for example:
binaries/brename_linux_amd64.tar.gz
binaries/brename_windows_386.exe.tar.gz
binaries/brename_windows_amd64.exe.tar.gz

$ brename -p '.gz$' -R -l -a
/home/shenwei/project/src/github.com/shenwei356/brename/binaries/brename_darwin_386.tar.gz
/home/shenwei/project/src/github.com/shenwei356/brename/binaries/brename_darwin_amd64.tar.gz
Expand Down
8 changes: 5 additions & 3 deletions brename.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"

"github.com/fatih/color"
Expand All @@ -42,7 +41,7 @@ import (

var log *logging.Logger

var version = "2.5.1"
var version = "2.5.2"
var app = "brename"

// Options is the struct containing all global options
Expand All @@ -69,6 +68,7 @@ type Options struct {

ReplaceWithNR bool
StartNum int
NRFormat string

ReplaceWithKV bool
KVs map[string]string
Expand Down Expand Up @@ -198,6 +198,7 @@ func getOptions(cmd *cobra.Command) *Options {

ReplaceWithNR: replaceWithNR,
StartNum: getFlagNonNegativeInt(cmd, "start-num"),
NRFormat: fmt.Sprintf("%%0%dd", getFlagPositiveInt(cmd, "nr-width")),
ReplaceWithKV: replaceWithKV,

KVs: kvs,
Expand Down Expand Up @@ -242,6 +243,7 @@ func init() {
RootCmd.Flags().IntP("key-capt-idx", "I", 1, "capture variable index of key (1-based)")
RootCmd.Flags().StringP("key-miss-repl", "m", "", "replacement for key with no corresponding value")
RootCmd.Flags().IntP("start-num", "n", 1, `starting number when using {nr} in replacement`)
RootCmd.Flags().IntP("nr-width", "", 1, `minimum width for {nr} in flag -r/--replacement. e.g., formating "1" to "001" by --nr-width 3`)

RootCmd.Example = ` 1. dry run and showing potential dangerous operations
brename -p "abc" -d
Expand Down Expand Up @@ -573,7 +575,7 @@ func checkOperation(opt *Options, path string) (bool, operation) {
r := opt.Replacement

if opt.ReplaceWithNR {
r = reNR.ReplaceAllString(r, strconv.Itoa(opt.StartNum))
r = reNR.ReplaceAllString(r, fmt.Sprintf(opt.NRFormat, opt.StartNum))
opt.StartNum++
}

Expand Down

0 comments on commit 9cc1d83

Please sign in to comment.