-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (67 loc) · 1.78 KB
/
main.go
File metadata and controls
77 lines (67 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
func main() {
// Get user input for path, old character, and new character
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter path to scan: ")
path, _ := reader.ReadString('\n')
path = strings.TrimSpace(path)
if path == "" {
path = "." // Default to current directory
}
// Convert to absolute path
absPath, err := filepath.Abs(path)
if err != nil {
fmt.Println("Error resolving absolute path:", err)
return
}
fmt.Print("Enter character(s) to replace (use SPACE for spaces): ")
oldChar, _ := reader.ReadString('\n')
oldChar = strings.TrimSpace(oldChar)
if oldChar == "SPACE" {
oldChar = " "
}
if oldChar == "" {
fmt.Println("You must specify a character to replace.")
return
}
fmt.Print("Enter replacement character(s) (use SPACE for spaces): ")
newChar, _ := reader.ReadString('\n')
newChar = strings.TrimSpace(newChar)
if newChar == "SPACE" {
newChar = " "
}
// Read only the contents of the specified directory
entries, err := os.ReadDir(absPath)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}
for _, entry := range entries {
// Rename only if it's a directory and contains the oldChar
if entry.IsDir() {
dirName := entry.Name()
if strings.Contains(dirName, oldChar) {
newDirName := strings.ReplaceAll(dirName, oldChar, newChar)
oldPath := filepath.Join(absPath, dirName)
newPath := filepath.Join(absPath, newDirName)
// Rename directory
err := os.Rename(oldPath, newPath)
if err != nil {
fmt.Printf("Error renaming folder '%s' to '%s': %v\n", oldPath, newPath, err)
} else {
fmt.Printf("Renamed '%s' to '%s'\n", oldPath, newPath)
}
}
}
}
if err != nil {
fmt.Println("Error scanning directory:", err)
}
}