-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0266574
Showing
6 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"console": "integratedTerminal", | ||
"program": "${fileDirname}", | ||
"args": [ | ||
// "-d", | ||
// "-r", | ||
// "-q", | ||
"./test/", | ||
"^t", | ||
"s" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2024 Salvatore Saba | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Recursive Rename tool | ||
The purpose of this command line program is to change the name of the given files using regular expressions. | ||
The syntax of the regular expressions accepted is the same general syntax used by Perl, Python, and other languages. More precisely, it is the syntax accepted by RE2 and described at [https://golang.org/s/re2syntax](https://golang.org/s/re2syntax), except for \C. For an overview of the syntax, see the [regexp/syntax](https://pkg.go.dev/regexp/syntax) package. | ||
|
||
Version 1.0 | ||
Usage: rr [options] <path> <match_rule> <replace_rule> | ||
**-r** = Search recursively in directories | ||
**-d** = Include directory names | ||
**-e** = Exclude file extension from the text matching | ||
**-f** = Force replacement if file already exists | ||
**-q** = Perform action without confirmation | ||
**-h** = Print this help | ||
|
||
Example: | ||
- `rr -r ./test "^t" "r"` | ||
Search recursively for all files in the test folder and rename those starting with "t" to "r" | ||
- `rr ./test '(\d+)' '$1$1'` | ||
Search for all files in the test folder and if they contain a number it is written twice ('test1.txt' -> 'test11.txt') | ||
- `rr -r -f ./test '\.JPG$' '.jpg'` | ||
Edit file extension overwriting existing files with the same name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module test/rr | ||
|
||
go 1.23.0 | ||
|
||
require github.com/schollz/progressbar/v3 v3.14.6 | ||
|
||
require ( | ||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect | ||
github.com/rivo/uniseg v0.4.7 // indirect | ||
golang.org/x/sys v0.22.0 // indirect | ||
golang.org/x/term v0.22.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= | ||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= | ||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= | ||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
github.com/schollz/progressbar/v3 v3.14.6 h1:GyjwcWBAf+GFDMLziwerKvpuS7ZF+mNTAXIB2aspiZs= | ||
github.com/schollz/progressbar/v3 v3.14.6/go.mod h1:Nrzpuw3Nl0srLY0VlTvC4V6RL50pcEymjy6qyJAaLa0= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= | ||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= | ||
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
var ( | ||
recursive = flag.Bool("r", false, "Search recursively in directories") | ||
includeDirs = flag.Bool("d", false, "Include directory names") | ||
excludeExt = flag.Bool("e", false, "Exclude file extension from the text matching") | ||
force = flag.Bool("f", false, "Force replacement if file already exists") | ||
noConfirm = flag.Bool("q", false, "Perform action without confirmation") | ||
help = flag.Bool("h", false, "Print this help ") | ||
) | ||
|
||
flag.Parse() | ||
|
||
if *help || flag.NArg() < 3 { | ||
printHelp() | ||
return | ||
} | ||
|
||
rootDir := flag.Arg(0) | ||
matchPattern := flag.Arg(1) | ||
replacePattern := flag.Arg(2) | ||
|
||
regex, err := regexp.Compile(matchPattern) | ||
if err != nil { | ||
fmt.Printf("Regular expression compilation error: %v\n", err) | ||
return | ||
} | ||
|
||
//create the target files list | ||
var filesToRename []string | ||
|
||
err = filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Esclude the root directory name | ||
if path == rootDir { | ||
return nil | ||
} | ||
|
||
if info.IsDir() { //check the directory | ||
if *includeDirs { //include or not the directory name | ||
//check the directory name | ||
dirName := filepath.Base(path) | ||
//add the file name to the list if it maches the given rule | ||
if regex.MatchString(dirName) { | ||
filesToRename = append(filesToRename, path) | ||
} | ||
} | ||
if !*recursive { | ||
return filepath.SkipDir | ||
} | ||
return nil | ||
} else { //check the files | ||
//check all the file name or the name without the extension | ||
oldName := filepath.Base(path) | ||
ext := filepath.Ext(oldName) | ||
nameToMatch := oldName | ||
if *excludeExt && !info.IsDir() { | ||
nameToMatch = strings.TrimSuffix(oldName, ext) | ||
} | ||
//add the file name to the list if it matches the given rule | ||
if regex.MatchString(nameToMatch) { | ||
filesToRename = append(filesToRename, path) | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
fmt.Printf("Error scanning directory: %v\n", err) | ||
return | ||
} | ||
|
||
if len(filesToRename) == 0 { | ||
fmt.Println("No files found for renaming.") | ||
return | ||
} | ||
|
||
var newPaths []string | ||
fmt.Printf("Found %d files to rename:\n", len(filesToRename)) | ||
for _, path := range filesToRename { | ||
relativePath, _ := filepath.Rel(rootDir, path) | ||
oldName := filepath.Base(relativePath) | ||
ext := filepath.Ext(oldName) | ||
|
||
nameToReplace := oldName | ||
if *excludeExt && (filepath.Ext(path) != "") { | ||
nameToReplace = strings.TrimSuffix(oldName, ext) | ||
} | ||
|
||
newName := regex.ReplaceAllString(nameToReplace, replacePattern) | ||
if *excludeExt && (filepath.Ext(path) != "") { | ||
newName += ext | ||
} | ||
newPath := filepath.Join(filepath.Dir(path), newName) | ||
|
||
newPaths = append(newPaths, newPath) //create the list of the new names | ||
newRelativePath, _ := filepath.Rel(rootDir, newPath) | ||
fmt.Printf("'%s' -> '%s'\n", relativePath, newRelativePath) | ||
} | ||
|
||
if !*noConfirm { | ||
fmt.Printf("Procede to rename %d files? (y/n):\n ", len(filesToRename)) | ||
reader := bufio.NewReader(os.Stdin) | ||
response, _ := reader.ReadString('\n') | ||
response = strings.ToLower(strings.TrimSpace(response)) | ||
|
||
if response != "y" { | ||
fmt.Println("Operation cancelled") | ||
return | ||
} | ||
} | ||
|
||
n_files := len(filesToRename) | ||
for i := 0; i < n_files; i++ { | ||
rename := true | ||
path := filesToRename[i] | ||
newPath := newPaths[i] | ||
if !*force { | ||
if _, err := os.Stat(newPath); err == nil { | ||
for { | ||
fmt.Printf("The file '%s' already exist. Do you wanto to replace it? (Yes/No/All): ", newPath) | ||
reader := bufio.NewReader(os.Stdin) | ||
response, _ := reader.ReadString('\n') | ||
response = strings.ToLower(strings.TrimSpace(response)) | ||
|
||
switch response { | ||
case "y": | ||
break | ||
case "a": | ||
*force = true | ||
break | ||
case "n": | ||
rename = false | ||
fmt.Println("Operation cancelled") | ||
break | ||
default: | ||
fmt.Println("Answer not valid") | ||
} | ||
} | ||
} | ||
} | ||
if rename { | ||
err := os.Rename(path, newPath) | ||
if err != nil { | ||
fmt.Printf("Error renaming '%s' -> '%s': %v\n", path, newPath, err) | ||
return | ||
} | ||
} | ||
} | ||
|
||
fmt.Println("Renaming completed successfully") | ||
fmt.Print("Press enter to close the program...") | ||
if !*noConfirm { | ||
bufio.NewReader(os.Stdin).ReadString('\n') | ||
} | ||
} | ||
|
||
func printHelp() { | ||
fmt.Print(` | ||
Version 1.0 | ||
Usage: rr [options] <match_rule> <replace_rule> | ||
-r = Search recursively in directories | ||
-d = Include directory names | ||
-e = Exclude file extension from the text matching | ||
-f = Force replacement if file already exists | ||
-q = Perform action without confirmation | ||
-h = Print this help | ||
Example: | ||
rr -r ./test "^t" "r" | ||
Search recursively for all files in the test folder and rename those starting with “t” to “r” | ||
rr ./test '(\d+)' '$1$1' | ||
Search for all files in the test folder and if they contain a number it is written twice (‘test1.txt’ -> ‘test11.txt’) | ||
rr -r -f ./test '\.JPG$' '.jpg' | ||
Edit file extension overwriting existing files with the same name | ||
`) | ||
} |