feat: Add flag to ignore extension
This commit is contained in:
parent
d9d715986b
commit
c05005b334
2 changed files with 82 additions and 7 deletions
38
main.go
38
main.go
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
@ -10,13 +11,20 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Add flag for ignoring extensions
|
||||||
|
ignoreExt := flag.Bool("ignore-ext", false, "Ignore file extensions when renaming")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
// Check command line arguments
|
// Check command line arguments
|
||||||
if len(os.Args) != 2 {
|
args := flag.Args()
|
||||||
fmt.Println("Usage: renamedit <directory path>")
|
if len(args) != 1 {
|
||||||
|
fmt.Println("Usage: renamedit [options] <directory path>")
|
||||||
|
fmt.Println("Options:")
|
||||||
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
dirPath := os.Args[1]
|
dirPath := args[0]
|
||||||
|
|
||||||
// Check if directory exists
|
// Check if directory exists
|
||||||
info, err := os.Stat(dirPath)
|
info, err := os.Stat(dirPath)
|
||||||
|
|
@ -46,11 +54,30 @@ func main() {
|
||||||
|
|
||||||
// Write filenames to temporary file
|
// Write filenames to temporary file
|
||||||
fileNames := make([]string, 0)
|
fileNames := make([]string, 0)
|
||||||
|
fileExts := make([]string, 0)
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if !file.IsDir() {
|
if !file.IsDir() {
|
||||||
fileName := file.Name()
|
fileName := file.Name()
|
||||||
fileNames = append(fileNames, fileName)
|
fileNames = append(fileNames, fileName)
|
||||||
|
|
||||||
|
// 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")
|
tmpFile.WriteString(fileName + "\n")
|
||||||
|
fileExts = append(fileExts, "") // Empty placeholder
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tmpFile.Close()
|
tmpFile.Close()
|
||||||
|
|
@ -112,7 +139,12 @@ func main() {
|
||||||
|
|
||||||
// Perform renaming
|
// Perform renaming
|
||||||
for i, oldName := range fileNames {
|
for i, oldName := range fileNames {
|
||||||
|
// Re-add extension if we were ignoring it
|
||||||
newName := newFileNames[i]
|
newName := newFileNames[i]
|
||||||
|
if *ignoreExt {
|
||||||
|
newName = newName + fileExts[i]
|
||||||
|
}
|
||||||
|
|
||||||
if oldName != newName {
|
if oldName != newName {
|
||||||
oldPath := filepath.Join(dirPath, oldName)
|
oldPath := filepath.Join(dirPath, oldName)
|
||||||
newPath := filepath.Join(dirPath, newName)
|
newPath := filepath.Join(dirPath, newName)
|
||||||
|
|
|
||||||
47
readme.md
47
readme.md
|
|
@ -2,12 +2,55 @@
|
||||||
|
|
||||||
Rename files with your favourite editor
|
Rename files with your favourite editor
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Build from source:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone git@github.com:js0ny/renamedit.git
|
git clone https://github.com/js0ny/renamedit.git
|
||||||
cd renamedit
|
cd renamedit
|
||||||
go build
|
go build
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
renamedit <dir>
|
renamedit [options] <directory>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 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.)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue