Click here to Skip to main content
13,863,201 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

11.9K views
383 downloads
18 bookmarked
Posted 16 Oct 2017
Licenced CPOL

Run TaskScheduler on Folder Change

, 18 Oct 2017
Rate this:
Please Sign up or sign in to vote.
Run Task scheduler task on folder content change

Introduction

Windows Task Scheduler does not allow run task on folder content change. But it allows run task on windows log event. This application uses this feature for run TaskScheduler task on folder change.

Features:

  • Console application, which can be run as Task Scheduler Task
  • Configurable more folders, every folder has its own Event ID for identification in Task Scheduler
  • Configurable timeout in seconds for waiting for end of changes
  • One Timer for one folder
  • Simple and functional

Background

Application uses .NET FileSystemWatcher. Because changes in folders should be more (a lot of changes), this application use Timer for waiting a few second for stopping folder changes. If folders content was changed and then it does not change for x seconds, the application makes write to windows log. Then Task Scheduler runs task.

Using the Code

Adding one FileSystemWatcher:

public OneListener(Folder folder, FoldersClass folders)
   {
       folder.Parent = folders;
       FileSystemWatcher watcher = new FileSystemWatcher();
       watcher.Path = folder.Path;
       /* Watch for changes in LastAccess and LastWrite times, and
       the renaming of files or directories. */
       watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
          | NotifyFilters.FileName | NotifyFilters.DirectoryName;
       var flt = folder.Filter;
       if (string.IsNullOrWhiteSpace(flt))
           flt = "*.*";
       watcher.Filter = flt;

       // Add event handlers.
       watcher.Changed += new FileSystemEventHandler(folder.OnFolderChanged);
       watcher.Created += new FileSystemEventHandler(folder.OnFolderChanged);
       watcher.Deleted += new FileSystemEventHandler(folder.OnFolderChanged);
       watcher.Renamed += new RenamedEventHandler(folder.OnFileInFolderRenamed);

       watcher.EnableRaisingEvents = true;
       Console.WriteLine($"Listener added {folder.Path}, filter: {flt}, EventId: {folder.EventID}");
   }

After any change of folder, Timer is created or restarted:

internal void OnFolderChanged(object source, FileSystemEventArgs e)
{
    TouchTimer();
}

internal void OnFileInFolderRenamed(object source, RenamedEventArgs e)
{
    TouchTimer();
}

Timer is elapsed, when folder content was changed and changes stopped for x seconds, then write to Windows log is made:

private void writeToLog()
   {
       Console.WriteLine($"{DateTime.Now} - {Path} changed");
       using (EventLog eventLog = new EventLog("Application"))
       {
           eventLog.Source = "Application";
           eventLog.WriteEntry($"FolderContentChangeListener change {this.LogId}",
                               EventLogEntryType.Information, this.EventID);
       }
   }

Configuration example:

{  "TimeoutInSeconds":"10",
   "Folders":[  
      {  
         "Path":"h:\\_\\NNDov",
         "Filter":"*.7z",
         "LogId":"NNDov",
         "EventID": "777"
      },
      {  
         "Path":"h:\\Downloads",
         "Filter":"*.exe",
         "LogId":"Dnl",
         "EventID": "778"
      },
      {  
         "Path":"z:\\_",
         "Filter":"*.*",
         "LogId":"zz",
         "EventID": "779"
      }
   ]
}

Task Scheduler Trigger definition example:

Sample of UI with information about changes:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

BMicka
Slovakia Slovakia
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionConsole application, which can be run as Task Scheduler Task Pin
kiquenet.com27-Sep-18 11:07
professionalkiquenet.com27-Sep-18 11:07 
GeneralMy vote of 5 Pin
David Pierson3-Nov-17 2:31
memberDavid Pierson3-Nov-17 2:31 
QuestionTask Scheduler Trigger definition example ? Pin
Reynier Lester20-Oct-17 12:49
professionalReynier Lester20-Oct-17 12:49 
QuestionSource Pin
kmkmahesh16-Oct-17 6:21
professionalkmkmahesh16-Oct-17 6:21 
AnswerRe: Source Pin
BMicka16-Oct-17 10:20
memberBMicka16-Oct-17 10:20 
QuestionCould not download source code file Pin
Mou_kol16-Oct-17 5:09
memberMou_kol16-Oct-17 5:09 
AnswerRe: Could not download source code file Pin
BMicka16-Oct-17 10:21
memberBMicka16-Oct-17 10:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 18 Oct 2017
Article Copyright 2017 by BMicka
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid