From c05005b334f4062185116e8f2fdd39dc9afab037 Mon Sep 17 00:00:00 2001 From: js0ny Date: Tue, 18 Mar 2025 12:50:22 +0000 Subject: [PATCH 1/3] feat: Add flag to ignore extension --- main.go | 40 ++++++++++++++++++++++++++++++++++++---- readme.md | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 529ed65..f32cf98 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "flag" "fmt" "os" "os/exec" @@ -10,13 +11,20 @@ import ( ) func main() { + // Add flag for ignoring extensions + ignoreExt := flag.Bool("ignore-ext", false, "Ignore file extensions when renaming") + flag.Parse() + // Check command line arguments - if len(os.Args) != 2 { - fmt.Println("Usage: renamedit ") + args := flag.Args() + if len(args) != 1 { + fmt.Println("Usage: renamedit [options] ") + fmt.Println("Options:") + flag.PrintDefaults() os.Exit(1) } - dirPath := os.Args[1] + dirPath := args[0] // Check if directory exists info, err := os.Stat(dirPath) @@ -46,11 +54,30 @@ func main() { // Write filenames to temporary file fileNames := make([]string, 0) + fileExts := make([]string, 0) + for _, file := range files { if !file.IsDir() { fileName := file.Name() fileNames = append(fileNames, fileName) - tmpFile.WriteString(fileName + "\n") + + // If ignoring extensions, split filename and extension + if *ignoreExt { + baseName := fileName + ext := "" + + // Get extension only if there is one + if dotIndex := strings.LastIndex(fileName, "."); dotIndex > 0 { + baseName = fileName[:dotIndex] + ext = fileName[dotIndex:] + } + + fileExts = append(fileExts, ext) + tmpFile.WriteString(baseName + "\n") + } else { + tmpFile.WriteString(fileName + "\n") + fileExts = append(fileExts, "") // Empty placeholder + } } } tmpFile.Close() @@ -112,7 +139,12 @@ func main() { // Perform renaming for i, oldName := range fileNames { + // Re-add extension if we were ignoring it newName := newFileNames[i] + if *ignoreExt { + newName = newName + fileExts[i] + } + if oldName != newName { oldPath := filepath.Join(dirPath, oldName) newPath := filepath.Join(dirPath, newName) diff --git a/readme.md b/readme.md index 3f6a661..22d92e0 100644 --- a/readme.md +++ b/readme.md @@ -2,12 +2,55 @@ Rename files with your favourite editor +## Installation + +Build from source: + ```bash -git clone git@github.com:js0ny/renamedit.git +git clone https://github.com/js0ny/renamedit.git cd renamedit go build ``` +## Usage + ```bash -renamedit -``` \ No newline at end of file +renamedit [options] +``` + +### Options + +- `-ignore-ext`: Ignore file extensions when renaming files. This will preserve the original extensions. + +### Examples + +Basic usage: +```bash +renamedit /path/to/directory +``` + +Ignore file extensions (useful for batch renaming while preserving extensions): +```bash +renamedit -ignore-ext /path/to/directory +``` + +## How It Works + +1. The program opens a temporary file in your preferred text editor +2. Edit the file names as needed +3. Save and exit the editor +4. The program will rename the files according to your edits + +## Environment Variables + +- `EDITOR`: Set this to your preferred text editor (defaults to vim) + +## Supported Editors + +The following editors are explicitly supported with wait flags: +- Visual Studio Code (code) +- Sublime Text (subl) +- Zed Editor (zeditor) +- Atom (atom) +- Gedit (gedit) +- And most terminal editors (vim, nano, etc.) From f7792fef66103737a4d77fe4e50d8297705701eb Mon Sep 17 00:00:00 2001 From: js0ny Date: Tue, 18 Mar 2025 13:20:19 +0000 Subject: [PATCH 2/3] Use github path for module --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 908cada..0cf77e3 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module renamedit +module github.com/js0ny/renamedit go 1.24.1 From e705b91d8e075af53a4a542f3097191250d11d65 Mon Sep 17 00:00:00 2001 From: js0ny Date: Wed, 22 Oct 2025 19:43:06 +0100 Subject: [PATCH 3/3] feat: Add shorthand flag --- main.go | 14 ++++++++++---- readme.md | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index f32cf98..8deb0b4 100644 --- a/main.go +++ b/main.go @@ -10,9 +10,15 @@ import ( "strings" ) +var ignoreExt bool + +func init() { + const ignoreExtDesc = "Ignore file extensions when renaming" + flag.BoolVar(&ignoreExt, "ignore-ext", false, ignoreExtDesc) + flag.BoolVar(&ignoreExt, "i", false, ignoreExtDesc+" (shorthand)") +} + func main() { - // Add flag for ignoring extensions - ignoreExt := flag.Bool("ignore-ext", false, "Ignore file extensions when renaming") flag.Parse() // Check command line arguments @@ -62,7 +68,7 @@ func main() { fileNames = append(fileNames, fileName) // If ignoring extensions, split filename and extension - if *ignoreExt { + if ignoreExt { baseName := fileName ext := "" @@ -141,7 +147,7 @@ func main() { for i, oldName := range fileNames { // Re-add extension if we were ignoring it newName := newFileNames[i] - if *ignoreExt { + if ignoreExt { newName = newName + fileExts[i] } diff --git a/readme.md b/readme.md index 22d92e0..46bc85c 100644 --- a/readme.md +++ b/readme.md @@ -31,7 +31,8 @@ renamedit /path/to/directory Ignore file extensions (useful for batch renaming while preserving extensions): ```bash -renamedit -ignore-ext /path/to/directory +renamedit -i /path/to/directory +renamedit --ignore-ext /path/to/directory ``` ## How It Works