Watching new files being made in a folder?

Hello!

I have a case in which new files are being made in a directory every x seconds. My question is how do I use Filewatching.watch_folder in such a way that is registers and prints to the terminal each newly made file? (File Events · The Julia Language)

I also found this repository:

Is this perhaps better to use, if I need it to catch all events?

Thank you.

Kind regards

Apparently no one really knows, if you are stuck on a similar point in the future I advise for now to have a look through this thread:

Or calling an external library you are familiar with.

I marked this as solution until/if someone provides a much better answer than what I did here :slight_smile:

Kind regards

I’m just familiarizing myself with FileWatching so please point out any errors. In case a simple working example is helpful this code modifies an example provided by tamasgal

function monitor( folder, watch = Ref(true))
   @async while watch[] 
        file, event =  watch_folder(folder)
        @show file, event 
    end
return watch
end 

One caveat here is that even after you set Ref = false

w = watch(folder) 

w[] = false

it will continue to @show the next file saved to the folder because the last iteration of the while loop has to terminate.

Also watch_folder returns the name of the file modified and an object with the boolean fields: renamed, changed, and timeout. Therefore when monitoring files being created in a folder in real time the same file will appear twice. Once when the file is created and event will = (true, false, false) and once when data is written to the file where event = (false, true, false). If you only want the file to be printed to the terminal once I think you need to do something like

function monitor2( folder, watch = Ref(true))
   @async while watch[] 
        file, event =  watch_folder(folder)
        event ==  FileWatching.FileEvent(false, true, false) && @show file event 
    end
return watch
end