File watcher in SSIS
Hi Everyone
I have read a lot about the filewatcher developed by Darren and how others have used it. My question is the same as others. How can we make the Package to run continuosly to see whether a file is there in the location and then execute the package?
With the answers i have read i have only two options.
1) Have a for each loop or a for loop in SSIS package to have the package checking for files. (If so what would be the condition and where should i use it)
2) Have a SQL Agent job ( I have no idea how this can be performed)
Please any one if u have a detail level explanation or a sample project regarding these two methods please share. I am new to this so it would be much grateful.
Thanks
Lucki
October 28th, 2010 3:44am
Lucki,
You need to implement a WMI event watcher task in SSIS. The nature of WMI Event watcher with a WQL(WMI Query Language) will take care of the rest.
You should find the following link useful:
http://msdn.microsoft.com/en-us/library/ms141130.aspxRegards, Raunak J
Free Windows Admin Tool Kit Click here and download it now
October 28th, 2010 5:10am
Hi Raunak
Thanks a load for the reply. I will use this process and try it. Also thanks for the link provided.
Thanks
Lucki
October 29th, 2010 12:56am
Hi Lucki,
In order to check if file is in a folder, and if so, execute a package, we have two options. One is using SQL Agent job, which is you mentioned. And the other one is implementing a Windows Service, which uses the same code as File watcher, and when a file
is created, execute a package.
For the first option, please set the interval of the job, then the package will be executed in a time period(interval). In the package, check if there is file created or changed.
For the second option, below is a code snippet for the Windows Service. In order to use it in this case, we need to implement the code used to execute a package in the events:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace FolderWatcherServices
{
public partial class FolderWatcherService : ServiceBase
{
static ProcessStartInfo startInfo;
public FolderWatcherService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// Create an FileSystemWatcherInformation to get the FileSystemWatcherInformation in the timer callback.
FileSystemWatcherInformation info = new FileSystemWatcherInformation();
FileWatcherEvent(info);
}
protected override void OnStop()
{
//timer.Dispose();
}
// This method is called by the timer delegate.
private static void FileWatcherEvent(object WatchingInfo)
{
string path = ((FileSystemWatcherInformation)WatchingInfo).path;
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = 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 | NotifyFilters.Size;
// Only watch text files.
watcher.Filter = "*.*";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
//Add code here to execute a package.
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
//Add code here to execute a package.
}
}
// Define a class to use as the object argument for the timer.
class FileSystemWatcherInformation
{
public string path = @"C:\Snapshot";
}
}
For more information, please see:
Running a Package Programmatically on the Local Computer:
http://msdn.microsoft.com/en-us/library/ms136090.aspx
If there is anything unclear, please feel free to ask.
Thanks,
Jin ChenJin Chen - MSFT
Free Windows Admin Tool Kit Click here and download it now
November 1st, 2010 4:56am
Jin,
I believe that using WQL, task would be much easier as the current scenario only invloves a file to be presentRegards, Raunak J Please 'Mark as Answer' if found helpful
November 1st, 2010 6:38am
Hi Raunak,
However, the WMI Event task can only run when the package is running.
Thanks,
Jin ChenJin Chen - MSFT
Free Windows Admin Tool Kit Click here and download it now
November 1st, 2010 7:19am
Jin,
WQL and Windows Service (A watcher in current context) will end up producing the same effect. I see not much difference. Both can be scored even.
Regards, Raunak J Please 'Mark as Answer' if found helpful
November 1st, 2010 7:40am