none
File System Watcher Filter for File Name Containing FOO

    Question

  • Hi, Everybody!

    I need a file system watcher (fsw) to filter any text files (*.txt) containing "foo" (for example) in the filename.  One might think "*foo*.txt" would work, but it does not.  Is this possible with fsw?  If so, how?

    BTW...  Does fsw support regex?

    Please advise.

    Monday, January 24, 2011 4:36 PM

Answers

  • Hey, For me it is working. I mean, for all files contain inf "foo", its working. Literally, I tried 222foo222.txt, foo222.txt, 222.txt and I get result for all. If you could post your code, it would be helpful to identify the problem. BTW here is my code.

    public partial class Form1 : Form
    {
     FileSystemWatcher watcher = new FileSystemWatcher();
    
     public Form1()
     {
      InitializeComponent();
      watcher.Path = @"D:\adavesh";
      watcher.Filter = "*foo*.txt";
      watcher.EnableRaisingEvents = true;
      watcher.Renamed += new RenamedEventHandler((o, e) => { UpdateTextBox(Environment.NewLine + e.Name + " : is Renamed"); });
      watcher.Changed += new FileSystemEventHandler((o, e) => { UpdateTextBox(Environment.NewLine + e.Name + " : is Changed"); });
      watcher.Created += new FileSystemEventHandler((o, e) => { UpdateTextBox(Environment.NewLine + e.Name + " : is Created"); });
      watcher.Deleted += new FileSystemEventHandler((o, e) => { UpdateTextBox(Environment.NewLine + e.Name + " : is Deleted"); });
     }
    
     private void UpdateTextBox(string text)
     {
      MethodInvoker mi = new MethodInvoker(() => { textBox1.Text += text; });
      this.Invoke(mi);   
     }
    }
    

     

    Tuesday, January 25, 2011 3:56 AM

All replies

  • Hi,

    according to msdn:

    http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.filter.aspx

    it should work:

    You can also watch for changes in a certain type of file. For example, to watch for changes in any text files, set the Filter property to "*.txt".

    examples:

    *recipe.doc All files ending in "recipe" with a "doc" extension.
    win*.xml    All files beginning with "win" with an "xml" extension.

    This isn't answer you are looking for, but you might have another issue in your code causing this issue.

    You could try to use simple Filter to check if it works otherwise properly.

    rob

    • Proposed as answer by Shah Bhavik Monday, January 24, 2011 6:49 PM
    Monday, January 24, 2011 6:19 PM
  • Try this code sample

    using

     

     

    System;

    using

     

     

    System.Collections.Generic;

    using

     

     

    System.Text;

    using

     

     

    System.Security.Principal;

    using

     

     

    System.Threading;

    using

     

     

    System.IO;

    namespace

     

     

    ConsoleApplication1

    {

     

     

    class Program

    {

     

     

    static void Main(string[] args)

    {

     

     

     

     

    FileSystemWatcher asd = new FileSystemWatcher(@"C:\");

    asd.Path =

     

    @"D:\";

    asd.Filter =

     

    "foo*.txt";

    asd.Changed +=

     

    new FileSystemEventHandler(asd_Changed);

    asd.Created +=

     

    new FileSystemEventHandler(asd_Changed);

    asd.Deleted +=

     

    new FileSystemEventHandler(asd_Changed);

    asd.Renamed +=

     

    new RenamedEventHandler(asd_Changed);

    asd.EnableRaisingEvents =

     

    true;

     

     

    Console.WriteLine("Press \'q\' to quit the sample.");

     

     

    while (Console.Read() != 'q') ;

     

    }

     

     

    static void asd_Changed(object sender, FileSystemEventArgs e)

    {

     

     

    Console.WriteLine("Name : " + e.Name);

    }

    }

    }

    Monday, January 24, 2011 6:53 PM
  • Hi, rob_v_

    Technically speaking, according to MSDN (http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.filter.aspx), it is silent regarding "*foo*.txt" (i.e., All *.txt files containing foo in the filename).  But as you pointed out, it does specify "foo*.txt" (i.e., All *.txt files beginning with foo in the filename) and "*foo.txt" (i.e., All *.txt files ending with foo in the filename).  Therefore, "*foo*.txt" might be equivalent to "All *.txt files beginning and ending with foo in the filename" which is simply "foo.txt".

    Thank-you for your input.

    Monday, January 24, 2011 8:05 PM
  • Hi, Shah!

    Your code handles changes to, creation, deletion and renaming of *.txt files beginning with foo in the filename.

    Thank-you nonetheless for your input.

    Monday, January 24, 2011 8:12 PM
  • Hey, For me it is working. I mean, for all files contain inf "foo", its working. Literally, I tried 222foo222.txt, foo222.txt, 222.txt and I get result for all. If you could post your code, it would be helpful to identify the problem. BTW here is my code.

    public partial class Form1 : Form
    {
     FileSystemWatcher watcher = new FileSystemWatcher();
    
     public Form1()
     {
      InitializeComponent();
      watcher.Path = @"D:\adavesh";
      watcher.Filter = "*foo*.txt";
      watcher.EnableRaisingEvents = true;
      watcher.Renamed += new RenamedEventHandler((o, e) => { UpdateTextBox(Environment.NewLine + e.Name + " : is Renamed"); });
      watcher.Changed += new FileSystemEventHandler((o, e) => { UpdateTextBox(Environment.NewLine + e.Name + " : is Changed"); });
      watcher.Created += new FileSystemEventHandler((o, e) => { UpdateTextBox(Environment.NewLine + e.Name + " : is Created"); });
      watcher.Deleted += new FileSystemEventHandler((o, e) => { UpdateTextBox(Environment.NewLine + e.Name + " : is Deleted"); });
     }
    
     private void UpdateTextBox(string text)
     {
      MethodInvoker mi = new MethodInvoker(() => { textBox1.Text += text; });
      this.Invoke(mi);   
     }
    }
    

     

    Tuesday, January 25, 2011 3:56 AM
  • Hi, Adavesh M!

    You know?...  The filter "*foo*.txt" does work!

    I must have had some leading or trailing space (or other white space character) embedded in the filter.

    Thank-you for verifying this for me!

    Wednesday, January 26, 2011 6:35 PM