Skip to content

Commit 1a56796

Browse files
committed
Add ability to filter filenames by regex
relates to #1
1 parent 7b57eb8 commit 1a56796

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ There are multiple ways to use this tool:
2424
## Options
2525

2626
- `batch-rename --no-numbers` will disable line number output.
27-
This means that each line contains only the file path without a preceding number, which is easier to edit in some cases.
27+
This means that each line contains only the file path without a preceding number, which can help if you need to search and replace numbers in filenames.
2828
Using this option has the downside that any line removal or insertion can mess up the mapping between old and new paths, which can mess up your filenames.
29+
- `batch-rename --regex "\.png$|\.jpg$"` will only include files that match with the regular expression `\.png\$|\.jpg\$`. In this example only files ending in `.png` or `.jpg` are included.
2930

3031
## Building
3132

@@ -36,4 +37,4 @@ Make sure to [install GoReleaser](https://goreleaser.com/install/).
3637
- To simulate the release process, use `goreleaser --skip-publish --auto-snapshot --clean`.
3738

3839
To build for your current platform and without GoReleaser, use `go build`.
39-
This will not include the version information.
40+
This will result in the same executable, but without correct version information.

main.go

+16
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212
"log"
1313
"os"
1414
"path/filepath"
15+
"regexp"
1516

1617
"github.com/skratchdot/open-golang/open"
1718
)
1819

1920
var flagNoNumbers = flag.Bool("no-numbers", false, "If set, batch-rename will not prepend numbers to every line. If you enable this option you have to make sure that you don't add or remove lines, as otherwise it will mess up your filenames!")
21+
var flagFilterRegex = flag.String("regex", "", "Filters entries with the specified regular expression by their filepath relative to the working directory. Example: batch-rename --regex \"\\.png$|\\.jpg$\", which will only include png and jpg files.")
2022

2123
func main() {
2224
flag.Parse()
@@ -26,6 +28,15 @@ func main() {
2628
rootDir := "."
2729
numbering := !*flagNoNumbers
2830

31+
var filterRegex *regexp.Regexp
32+
if *flagFilterRegex != "" {
33+
var err error
34+
if filterRegex, err = regexp.Compile(*flagFilterRegex); err != nil {
35+
log.Printf("Invalid regular expression: %v", err)
36+
return
37+
}
38+
}
39+
2940
fileEntries := []fileEntry{}
3041

3142
err := filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
@@ -35,6 +46,11 @@ func main() {
3546

3647
switch d.IsDir() {
3748
case false:
49+
50+
if filterRegex != nil && !filterRegex.Match([]byte(path)) {
51+
return nil
52+
}
53+
3854
fileEntries = append(fileEntries, fileEntry{
3955
name: d.Name(),
4056
originalPath: path,

0 commit comments

Comments
 (0)