-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new file creation return a folder info #73
Comments
The event struct contains all the information your need using file := fileInfo{
Size: event.Size(),
Path: event.Path,
Name: event.Name(),
ModTime: event.ModTime(),
Mode: event.Mode(), IsDir: event.IsDir()} package main
import (
"fmt"
"log"
"time"
"github.com/radovskyb/watcher"
)
func main() {
w := watcher.New()
// SetMaxEvents to 1 to allow at most 1 event's to be received
// on the Event channel per watching cycle.
//
// If SetMaxEvents is not set, the default is to send all events.
w.SetMaxEvents(1)
// Only notify rename and move events.
w.FilterOps(watcher.Rename, watcher.Move)
// Only files that match the regular expression during file listings
// will be watched.
r := regexp.MustCompile("^abc$")
w.AddFilterHook(watcher.RegexFilterHook(r, false))
go func() {
for {
select {
case event := <-w.Event:
// THIS ---> event
fmt.Println(event) // Print the event's info.
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
// Watch this folder for changes.
if err := w.Add("."); err != nil {
log.Fatalln(err)
}
// Watch test_folder recursively for changes.
if err := w.AddRecursive("../test_folder"); err != nil {
log.Fatalln(err)
}
// Print a list of all of the files and folders currently
// being watched and their paths.
for path, f := range w.WatchedFiles() {
fmt.Printf("%s: %s\n", path, f.Name())
}
fmt.Println()
// Trigger 2 events after watcher started.
go func() {
w.Wait()
w.TriggerEvent(watcher.Create, nil)
w.TriggerEvent(watcher.Remove, nil)
}()
// Start the watching process - it'll check for changes every 100ms.
if err := w.Start(time.Millisecond * 100); err != nil {
log.Fatalln(err)
}
} |
@Akumzy sir, you need to "edit" the file to get file information, if this is a first time you write a file, you just only get Folder info from |
|
Yeah is true when an event occurs at first two different events are being emitted one from the parent directory while the second one will be the real event source |
So I used this to filter it out if event.IsDir() && event.Op == watcher.Write {
continue
} |
@Akumzy thank you |
umm actually, i still need to "modify" the file first, then i got the file info, what i need is i get File info when in the first file creation because currently you will get folder info in first file creation |
@codenoid |
@Akumzy I've just been having a look into this library. I'm confused as to why the Create operation isn't being fired here instead of Write. If I touch or vim a file for the first time. It never fires a create event. |
@liamsorsby did you set |
If so it will likely emit |
@Akumzy I’ve tried with and without. I’ve also tried on different OS’s as I’m aware of an issue on mac’s not working correctly with fsnotifier in node either. If I just listen on Create nothing gets logged ever. |
Met the same problem: I want to get the Did anyone solve it? /cc @Akumzy |
@FogDong if you're not using the CLI you can use this to filter that out if event.IsDir() && event.Op == watcher.Write {
continue
} but for CLI you might want to fork the repo and add this since the author hasn't been active throughout this year |
DIRECTORY
FILE
how do i get File info (as
FILE
) for new file (like point 2), thank you !The text was updated successfully, but these errors were encountered: