-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
83 lines (77 loc) · 1.53 KB
/
file.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
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
79
80
81
82
83
package kitgo
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
//-------------------------------------
//
//
//
//-------------------------------------
func FileRenameTo(filePath string, newName string) (string, error) {
dir := filepath.Dir(filePath)
newPath := dir + "/" + newName
err := os.Rename(filePath, newPath)
if err != nil {
ErrorLog.Printf("[ %s ]重命名失败 => [ %v ]", filePath, err)
return "", err
}
return newPath, nil
}
//-------------------------------------
//
//
//
//-------------------------------------
func FilePathJoin(args ...string) string {
s := string(os.PathSeparator)
return strings.Join(args, s)
}
//-------------------------------------
//
//
//
//-------------------------------------
func FilePathToFileName(filePath string) string {
s := strings.LastIndexAny(filePath, string(os.PathSeparator))
if s >= 0 {
filePath = filePath[s + 1:]
}
e := strings.Index(filePath, ".")
if e >= 0 {
filePath = filePath[:e]
}
return filePath
}
//-------------------------------------
//
//
//
//-------------------------------------
func FileListPaths(dir string) ([]string, error) {
d, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
lf := []string{}
for _, info := range d {
// 完整路径
p := dir + string(os.PathSeparator) + info.Name()
//
if info.IsDir() {
// 目录
subFileList, err := FileListPaths(p)
if err != nil {
return nil, err
} else {
lf = append(lf, subFileList...)
}
} else {
// 文件
lf = append(lf, p)
}
}
return lf, nil
}