-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
55 lines (51 loc) · 1.33 KB
/
main.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
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/softlandia/cpd"
)
//выводит кодировку всех файлов с указанным расширением
//пример запуска: >detect-all-files .txt
//using: >detect-all-files .txt
func main() {
var fl []string
FindFilesExt(&fl, ".\\", os.Args[1])
for _, fn := range fl {
t, _ := cpd.FileCodepageDetect(fn)
fmt.Printf("file: \t`%s`\t`%s`\n", fn, t)
}
}
//FindFilesExt - search all files in path with 'ext' & put to list
//path - "c:\tmp"
//ext - ".log"
//sample: n, err := FindFilesExt(&fl, "c:\\tmp", ".log")
func FindFilesExt(fileList *[]string, path, fileNameExt string) (int, error) {
if fileList == nil {
return 0, errors.New("first parameter 'fileList' is nil")
}
extFile := strings.ToUpper(fileNameExt)
i := 0 //index founded files
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if info.IsDir() {
//skip folders
return nil
}
if strings.ToUpper(filepath.Ext(path)) != extFile {
//skip folders and files with extention not extFile
return nil
}
//file found
i++
*fileList = append(*fileList, path)
return nil
})
return i, err
}