fsnotify库
安装
go get github.com/fsnotify/fsnotify
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"github.com/fsnotify/fsnotify"
"io/ioutil"
"log"
"os"
"strings"
"time"
)
func ListenFolderNew() {
fmt.Print("-----文件夹监听-------")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done2 := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
fmt.Println("***********event")
if !ok {
return
}
fmt.Println("event.Op=>%#v", event.Op)
fmt.Println("文件操作类型判断是不是新建一个文件:%#v", event.Op&fsnotify.Create == fsnotify.Create)
if event.Op&fsnotify.Create == fsnotify.Create {
fmt.Println("*Create**event")
fmt.Println("新的文件:", event.Name)
mate := strings.Split(event.Name, "\\")
fileName := mate[len(mate)-1]
}
case err := <-watcher.Errors:
log.Println("error:", err)
case <-time.After(60 * time.Second):
continue
}
}
}()
err = watcher.Add("D:/file")
if err != nil {
log.Fatal(err)
}
<-done2
}