-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenamer.go
executable file
·52 lines (43 loc) · 938 Bytes
/
renamer.go
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
package main
import (
"bufio"
"log"
"os"
"strings"
)
func main() {
if len(os.Args) != 3 {
log.Fatal("Usage: renamer listA listB")
}
fA, err := os.Open(os.Args[1])
if err != nil {
log.Fatalf("Error opening %s: %v\n", os.Args[1], err)
}
defer fA.Close()
fB, err := os.Open(os.Args[2])
if err != nil {
log.Fatalf("Error opening %s: %v\n", os.Args[2], err)
}
defer fB.Close()
sA := bufio.NewScanner(fA)
sB := bufio.NewScanner(fB)
for {
if !sA.Scan() || !sB.Scan() {
break
}
lineA := strings.TrimSpace(sA.Text())
lineB := strings.TrimSpace(sB.Text())
if lineA == lineB {
continue
}
if err := os.Rename(lineA, lineB); err != nil {
log.Printf("Error renaming %s -> %s: %v\n", lineA, lineB, err)
}
}
if err := sA.Err(); err != nil {
log.Fatalf("Error reading %s: %v\n", os.Args[1], err)
}
if err := sB.Err(); err != nil {
log.Fatalf("Error reading %s: %v\n", os.Args[2], err)
}
}