-2

I told my FileWatcher, that it should include Subdirectories

_watcher.IncludeSubdirectories = true;

The problem, that I have is, that there are multiple files with the same file-ending. So my watcher gets triggered multiple times.

I have something like this

(subfolder[x] is the watched one)

mainFolder/subfolder[x]/ -> in this one example.mkv and two other filetypes mainFolder/subfolder[x]/subfolder2/ -> in this one is just one sample.mkv

FileWatcher is configured to:

_watcher.Filter = "*.mkv";

Currently the "created" event gets triggered two times ("example.mkv" and "sample.mkv") I would like to have it just triggered once for the "example.mkv" and not for the "sample.mkv". How can I just "watch" the first subfolder and exclude the second subfolder?

trashr0x
  • 6,457
  • 2
  • 29
  • 39
RedP1ll
  • 3
  • 4
  • Consider creating two watchers, one for each folder, both with `IncludeSubdirectories` set to `false`. – mjwills Mar 23 '18 at 12:28
  • Are you saying you only want to watch one sub-directory deep? As in you're watching mainFolder but the file will show up in one or more sub directories but sub-sub-directories should not be watched? On the other had if you're just watching subfolder1 then why are you setting `IncludeSubdirectories` in the first place? – juharr Mar 23 '18 at 12:31
  • There are multiple subdirectories1 with different names I don't know and I won't know. That is the problem. That's why I need Include Subdirectories = true. These subdirectories1 will get created through other external scripts/programs – RedP1ll Mar 23 '18 at 12:38
  • You can't. You need to think of logic to determine what to do when the files are created in the subfolders. It either raises events for all subfolders, or none - there's no logic other than what you apply yourself. – Reinstate Monica Cellio Mar 23 '18 at 12:46
  • If you can explain what you want the end result to be then we may be able to suggest something better. Don't worry about the fact that the subject matter is illegally downloaded films. Just explain better and we can help. – Reinstate Monica Cellio Mar 23 '18 at 12:48
  • @Archer I will try my best: I have a folder named "Series" (that is the main folder) A tool downloads multiple .rars and extracts them to "Series/SeriesnameSubfolderIncludingSeriesEpisodeName" (which is the reason I can't know the foldername) In these .rars there are following files: seriesnameSeasonEpisode.mkv; blahblah.nfo; blahblah.sfv; ->subfolder called "sample" sample.mkv; I just need the event getting triggered for seriesnameSeasonEpisode.mkv – RedP1ll Mar 23 '18 at 13:05
  • Can you not simply ignore the event when it's for the sample folder? That seems like the easiest solution to me. – Reinstate Monica Cellio Mar 23 '18 at 13:08
  • @Archer how do I do that? I tried with RegEx name-filtering etc. etc. but I don't find any solution. – RedP1ll Mar 23 '18 at 13:22
  • See the answer I posted below - I just tested that and it works fine for me. – Reinstate Monica Cellio Mar 23 '18 at 13:39
  • This is a matter of setting up the `FileSystemWatcher` the right way. If your `NotifyFilters` are just `NotifyFilters.FileName | NotifyFilters.CreationTime`, you won't be notified if a Directory is created. If you add `NotifyFilters.DirectoryName` you will. Note that you can setup 2 `FileSystemWatchers` classes that monitors 2 different kind of events. Also, the `OnCreated` event `e.Fullpath` parameter can be passed to a `FileInfo` class to test the attributes of that file name, to see if it has the Attribute "Directory". – Jimi Mar 23 '18 at 14:44
  • @Jimi okay, thank you. I will try setting up the right Notify Filters and add a second FileSystemWatcher later. :) – RedP1ll Mar 23 '18 at 14:51
  • Only if needed. If you only care about the file creation, you don't need anything else. If you are on Winforms, set FSW `.SynchronizingObject = this;` where `this` is the Form where the events are collected. – Jimi Mar 23 '18 at 14:52

1 Answers1

-1

You want to only Watch the target and 1st Order Child Directories/first two levels of the directory tree. Close to 0 code that is able to include subdirectories recusively on a Windows ever had such an ability. It was either "incldue all subdirectories" or "include no subdirectories".

Only code as modern as Robocopy has any ability to define the "depth". And FileWatcher is not nearly that modern. It is (t)rusty like hte Office COM Interop. It is still following the old "all Subdirectories or None" pattern. So you still have to use the old workaround - manual recursion:

  1. Do not use IncludeSubdirectories = true;
  2. Start a normal, non recursive File watcher on the root directory
  3. Itterate over all subdirectories manually. The Directory class should have you covered
  4. Start a seperate, non-recursive watcher for each subdirectory

Unfortuantely FileWatcher is not the most advanced class. For example figuring out wich kind of changed happened is nigh impossible. Wich is why someone took 9 watches with Filters and put them into one class, to get some decent level of information out of this class: https://www.codeproject.com/Articles/58740/FileSystemWatcher-Pure-Chaos-Part-of

Christopher
  • 9,634
  • 2
  • 17
  • 31