Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (61 loc) · 1.55 KB
/
Copy pathmain.go
File metadata and controls
75 lines (61 loc) · 1.55 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
)
func main() {
// Define the root directory to scan
diskName := flag.String("disk", "C", "the disk name, suche as 'C'")
flag.Parse()
root := fmt.Sprintf("%s:\\", *diskName)
// Define the list of file extensions to filter
extFilter := []string{}
// Create a channel to receive file paths
files := make(chan string)
// Create a channel to signal when all workers are done
done := make(chan bool)
// Define the number of worker goroutines to use
numWorkers := 10
start := time.Now()
// Start the workers
for i := 0; i < numWorkers; i++ {
go func() {
for filePath := range files {
ext := filepath.Ext(filePath) // TODO 增加非后缀类型的
// TODO 内部再次增加 异步循环,比对多个 extFilter 组
for _, filter := range extFilter {
if ext == filter[1:] {
fmt.Printf("TARGET is %s\n", filePath)
break
}
}
}
done <- true
}()
}
// Traverse the root directory recursively and send file paths to the channel
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("ERROR is %v\n", err)
//return err
}
if !info.IsDir() {
//fmt.Println("----", path)
files <- path
}
return nil
})
if err != nil {
fmt.Printf("ERROR is %v\n", err)
}
// Close the channel to signal no more files will be sent
close(files)
// Wait for all workers to finish
for i := 0; i < numWorkers; i++ {
<-done
}
fmt.Printf("Finish Tasks Cost=[%v]\n", time.Since(start))
}