# Watching new files being made in a folder?

**URL:** https://discourse.julialang.org/t/watching-new-files-being-made-in-a-folder/86444
**Category:** New to Julia
**Tags:** question
**Created:** [August 27, 2022, 9:52pm UTC](https://discourse.julialang.org/t/watching-new-files-being-made-in-a-folder/86444 "2022-08-27T21:52:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [August 27, 2022, 9:52pm UTC](https://discourse.julialang.org/t/watching-new-files-being-made-in-a-folder/86444/1 "2022-08-27T21:52:51Z")

</div>

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](https://docs.julialang.org/en/v1/stdlib/FileWatching/#FileWatching.watch_folder))

I also found this repository:

> **[GitHub - JuliaPluto/BetterFileWatching.jl: FileWatching but based on...](https://github.com/JuliaPluto/BetterFileWatching.jl)**
>
> FileWatching but based on Deno.watchFs. Contribute to JuliaPluto/BetterFileWatching.jl development by creating an account on GitHub.

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

Thank you.

Kind regards

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [August 30, 2022, 8:31pm UTC](https://discourse.julialang.org/t/watching-new-files-being-made-in-a-folder/86444/2 "2022-08-30T20:31:04Z")

</div>

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:

> [@How to stop a file watching task in Julia 1.8? Possible regression?](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/11):
>
> Thank you very much! You are indeed right that renaming = new file, even though it annoys me not being able to discern between modified name and actual new file. Still for now it is workable for me. Thank you for your help and time - it lets me progress my general code now. I do hope though that this becomes much better, because I find it so unbelievable that a language as Julia, which is good at so many things, actually struggles to properly close down subtasks and perform IO operations - th…

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 🙂

Kind regards

---

<div class="post-metadata">

### Author: ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)
#### Post date: [June 2, 2023, 3:32am UTC](https://discourse.julialang.org/t/watching-new-files-being-made-in-a-folder/86444/3 "2023-06-02T03:32:14Z")

</div>

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](https://stackoverflow.com/questions/76199201/how-to-run-a-function-on-each-file-in-a-folder-only-once-in-julia)

```julia
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`

```julia
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`](https://docs.julialang.org/en/v1/stdlib/FileWatching/) 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

```julia
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

```
