Trigger SSIS package when files available in a Folder

Recently I have goon through a scenario where my client needs to run the SSIS package when the files placed in folder. To run the package automatically when the file exists we have an external tool called FileWatcherTask which is a great user friendly (be aware that FileWatcherTask can cause memory leak and it’s been discussed in the SSIS community. It sounds like you can avoid using FWT altogether) but we can find similar task in Control Flow Tool box, which is WMI Event watcher task.

Different Approaches to run the Package when file exists.

  • Use the WMI Event Watcher task to trigger the SSIS packages as files arrive.
  • Create a Windows service that uses WMI to detect file arrival and launch packages.

Let’s discuss each of the available approach.

In this post I would like to show how to WMI Event Watcher task and other approaches to run SSIS package automatically when a file exists.

WMI Event Watcher Task:

WMI Event Watcher Task makes SSIS to wait for and respond to certain WMI events that occur in the operating System.

Scenarios where we can use WMI Event Watcher Task:

  • To watch a directory for a certain file to be written.
  • To watch for the CPU to be free.
  • Wait for a given service to start.
  • Wait for the memory of a server to reach a certain level before executing the rest of the package or before transferring files to the server.

The task uses a subset of SQL language which is WQL language; it allows us to view information through WMI.

My example is moving a file from Source folder to Archive folder with a FileSystem Task, WMI Event Watcher will pass the execution to FileSystem task when a file placed in Source Folder.

I took two variables to pass the Source Folder path dynamically.

1. User:: Quote = “

2. User::SourcePath = F:\\\\DellMeSoon\

Drag WMI Event Watcher task to the Control Flow Design pane, double click on the task.

In WMI Event Watcher Task Editor, we will need to create WMI Connection Manager, leave the Server name and Namespace options and select the check box to Use Windows Authentication

Click on Test button to test the Connection.

image

Select WqlQuerySourceType as DirectInput and for

WqlQuerySource keep . (dot)

image

Under Expressions tab and select WQLQuerySource property and use the following WQL Query as Expression.

Expression:

"SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance ISA "+@[User::Quote]+ "CIM_DirectoryContainsFile" + @[User::Quote]+"

AND TargetInstance.GroupComponent= "+@[User::Quote]+ "Win32_Directory.Name=\\" + @[User::Quote]+ @[User::SourcePath] + @[User::Quote]+ @[User::Quote]

There are three distinct classes:

  • __InstanceCreationEvent class, representing creation of a new object of a WMI class,
  • __InstanceDeletionEvent class, representing deletion of an existing object of a WMI class,
  • __InstanceModificationEvent class, representing modification to an existing object of a WMI class.

These classes are designed with the intention of being paired up with another WMI class, providing the ability to monitor changes to its objects.

WITHIN keyword set the interval between the event taking place and its notification being delivered to the WMI-based application (WMI Event Task, in this case).

WHERE clause includes the ISA keyword followed by the name of a monitored class.

image

Other properties includes:

ActionAtEvent: Specify whether the WMI event logs the event and initiates an SSIS action, or only logs the event.

AfterEvent: Specify whether the task succeeds or fails after it receives the WMI event, or if the task continues watching for the event to occur again.

ActionAtTimeout: Specify whether the task logs a WMI query time-out and initiates an SSIS event in response, or only logs the time-out.

AfterTimeout: Specify whether the task succeeds or fails in response to a time-out, or if the task continues watching for another time-out to recur.

NumberOfEvents: To specify the number of events to watch for.

Timeout: Specify the number of seconds to wait for the event to occur. A value of 0 means that no time-out is in effect.

Drag FileSystem task to move the File from Source folder to the Archive folder once available.

image

Now, execute the package. WMI Event watcher task will start watching the Source Folder till the file available. The task will be in execution mode and watch the source folder for every nth time which we gave in the WQL Query.

image

When the file is placed in the source folder WMI Event Watcher task will pass the execution to the next connected task.

image

Keep the package in Infinity loop for continuous watching of Source Folder.

Lets discuss about Windows Service Approach in my next post.

About these ads

10 Comments (+add yours?)

  1. Trackback: Trigger SSIS package when files available in a Folder part#2 « MSBI Mentalist
  2. Trackback: Link Resource # 51 : May 15 – May 28 « Dactylonomy of Web Resource
  3. Trackback: Link Resource # 51 : May 01 – May 06 « Dactylonomy of Web Resource
  4. Anonymous
    Sep 24, 2012 @ 10:21:28

    When i execute the WMI Event Task its not progressing to green even after dropping file in the folder.Its only in the validation mode.Please reply

    Reply

    • Sunilreddy
      Sep 25, 2012 @ 14:50:58

      Hi, You need to start the SSIS package first than move the file into the folder, it will work if the path is pointing to the folder.
      Can I know the Expressions you are passing to the properties…

      Thanks
      Sunil Kumar

      Reply

      • Uma
        Oct 08, 2012 @ 11:46:43

        Hi ,
        In this expression Editor ,What type of Expression iam useing?

      • Uma
        Oct 08, 2012 @ 11:50:24

        I am getting this error

        [WMI Event Watcher Task] Error: Watching for the Wql query caused the following system exception: “Unparsable query.”. Check the query for errors or WMI connection for access rights/permissions.

  5. Anonymous
    Oct 08, 2012 @ 11:39:40

    Hi ,

    Can i konw which Expression iam useing?

    Vasu

    Reply

  6. Anonymous
    Oct 23, 2012 @ 18:26:48

    I tried your suggestion about deleting the file, executing the task, and then readding the file to the directory and the task still remains yellow. I have to manually kill it.

    Reply

  7. Imran Mohammed
    Mar 28, 2013 @ 14:53:21

    I was getting WMI connection for access right/permission error,

    I got below from microsoft website,

    SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance ISA “CIM_DirectoryContainsFile” and TargetInstance.GroupComponent= “Win32_Directory.Name=\”c:\\\\WMIFileWatcher\”"

    Reference: http://msdn.microsoft.com/en-us/library/ms141130.aspx

    Reply

Leave a Reply...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 410 other followers

%d bloggers like this: