-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize.go
More file actions
78 lines (68 loc) · 1.85 KB
/
serialize.go
File metadata and controls
78 lines (68 loc) · 1.85 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
78
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
const defaultDirectory = ""
const defaultExtension = ""
const defaultHeader = ""
const defaultFooter = ""
const defaultDigits = 0
type arguments struct {
directory string
extension string
header string
footer string
digits int
isTest bool
}
func main() {
args := fetchArgs()
if args.extension != "" && args.extension[0] != '.' {
args.extension = "." + args.extension
}
fileInfos, err := ioutil.ReadDir(args.directory)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("<-Before\t\tAfter->")
serial := 0;
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
continue
}
if args.extension != "" && filepath.Ext(fileInfo.Name()) != args.extension {
continue
}
format := "%s%"
if args.digits != 0 {
format += fmt.Sprintf("0%d", args.digits)
}
format += "d%s%s"
newname := fmt.Sprintf(format, args.header, serial, args.footer, filepath.Ext(fileInfo.Name()))
fmt.Printf("%s\t\t%s\n", fileInfo.Name(), newname)
if !args.isTest {
oldpath := filepath.Join(args.directory, fileInfo.Name())
newpath := filepath.Join(args.directory, newname)
if err := os.Rename(oldpath, newpath); err != nil {
fmt.Println("Error:", err)
}
}
serial++
}
}
func fetchArgs() *arguments {
args := new(arguments)
flag.StringVar(&args.directory, "d", defaultDirectory, "Path of directory.")
flag.StringVar(&args.extension, "e", defaultExtension, "Condition of file extension.")
flag.StringVar(&args.header, "h", defaultHeader, "Header string of finename.")
flag.StringVar(&args.footer, "f", defaultFooter, "Footer string of filename.")
flag.IntVar(&args.digits, "n", defaultDigits, "Number of digits for serial number.")
flag.BoolVar(&args.isTest, "t", false, "Output new file name only (will not rename).")
flag.Parse()
return args
}