Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
SolvedPrivate

SSIS 2008 C# Script Task to LOOP for a set time until a file is detected in a folder

Posted on 2014-02-18
14
67 Views
Last Modified: 2016-02-10
I have the following C# SSIS 2008 Script Task
The task checks for the existence of a file in a specific location, if the file exists the ‘else’ statement is executed otherwise  the script fails
public void Main()
        {
            Dts.TaskResult = (int)ScriptResults.Success;
            String[] SemFiles = Directory.GetFiles(Dts.Variables["SemFilePath_dev"].Value.ToString(), "CDPreBatchEnded*");    
            if (SemFiles.Length != 1) 
                Dts.TaskResult = (int)ScriptResults.Failure;
            else 
                File.Create(SemFiles[0].Replace("BatchEnded", "ExtractStarted"));
        }

Open in new window

I want to amend this script so that if the file is not present , the script will try again every five minutes  a total of 12 times. If the file is still not present after 12 attempts I want the script to fail
I am currently looking into the WHILE loop and SLEEP  statements, any guidance appreciated.
Thanks
0
Comment
Question by:blossompark
  • 10
  • 3
14 Comments
 

Author Comment

by:blossompark
ID: 39867821
I am currently experimenting with a For Loop Container
0
 
LVL 37

Accepted Solution

by:
ValentinoV earned 375 total points
ID: 39869579
There's an interesting .NET object which you could make use of here.  It's been developed specifically for this kind of file logic, it's called FileSystemWatcher.

Using the FileSystemWatcher.WaitForChanged (WatcherChangeTypes, Int32) method you can specify how long the watcher needs to "watch".
0
 

Author Comment

by:blossompark
ID: 39869669
Hi ValentinoV
Thanks for your response, will study this and update later
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 125 total points
ID: 39870973
I'd recommend the FileSystemWatcher as well, coupled with a Threading.Timer to wait the acceptable interval before failing. I've used the watcher before and had it do exactly what I expected - I've only run into a problem when watching a folder with literally a million files in it.
0
 

Author Comment

by:blossompark
ID: 39872842
Hi ValentinoV and  ryanmccauley,
Thanks for your comments.
I am going down the FileSystemWatcher Class route and I would be grateful for some advice…
I have very little experience with  scripting, so  firstly, I am trying to “cobble” together  a script based on the Microsoft site…
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx
I have attached my script  and a screen grab which hopefully highlights the issues I am experiencing  a little clearer, all those red “squiggles”.
Basically what I am trying to do is;
Create a FileSystemWatcher instance named “watcher”.
Watch the folder path in the variable “SemFilePath_dev” .
Filter on file name beginning with “CDPreBatchEnded”
When a file beginning with the name “CDPreBatchEnded” arrives in the folder,
then create a copy of the file renaming the “CDPreBatchEnded” portion of the filename with “ODSExtractStarted”
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;

namespace ST_07bc0354bae645e8bf2e65be651aeec0.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

     
        public void Main()
        {
          {
    Run();

    }

public static void Run()

            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();

            //Watch for changes in the following directory
            watcher.Path=Dts.Variables["SemFilePath_dev"].Value.ToString();

            /* Watch for changes in  LastWrite times in the  directory. */
            watcher.NotifyFilter = NotifyFilters.LastWrite;

          //  Only watch for the following filename pattern
            watcher.Filter = "CDPreBatchEnded*";

            // Add event handlers.
            watcher.Created += new FileSystemEventHandler(OnChanged);
                // Begin watching.
        watcher.EnableRaisingEvents = true;
    }   
            // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is  created, 
         File.Create(e.Replace("CDPreBatchEnded", "ODSExtractStarted"));
    }

        }
    }
}

Open in new window

watcher code
I have been playing around with this for a while, trying to get rid of those red “squiggles” but I am only guessing, I don’t really understand the logic behind the constructs well enough to make educated guesses.

The message when I rollover the “=” sign is
Invalid token ‘=’ in class struct or interface member declaration


The message when I rollover the  "SemFilePath_dev" is
Identifier expected

The message when I rollover the  " FileSystemEventHandler" is
Method must have a return type

I will  continue to work on resolving these issues. I f you have any advice it will be greatly appreciated. Thanks
0
 

Author Comment

by:blossompark
ID: 39872912
commenting out the following
//          {
//    Run();

//    }

//public static void Run()

Open in new window

seems to have improved things
now issue with
    File.Create(e.Replace("CDPreBatchEnded", "ODSExtractStarted"));

Open in new window

when i rollover i get
System.IO.FileSystemEventArgs does not contain a definition for 'Replace'.....

also 1 red squiggly line remaining on very  last curly bracket
0
 

Author Comment

by:blossompark
ID: 39872916
deleting curly bracket , now  only obvious issue is with 'Replace' in
    File.Create(e.Replace("CDPreBatchEnded", "ODSExtractStarted"));

Open in new window

0
 

Author Comment

by:blossompark
ID: 39872943
Hi,
I have  modified the event handler to display a message box
  private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is  created, 
        // File.Create(e.Replace("CDPreBatchEnded", "ODSExtractStarted"));
         MessageBox.Show("Hello, File exists");
    }

Open in new window


The script task now runs to completion sucessfully but the message box is not displayed.
I think I am missing some code to initialise the watcher instance?

current code I am running is below
 public void Main()
        {
           // MessageBox.Show("Hello, File exists");
//          {
//    Run();

//    }

//public static void Run()

            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();

            //Watch for changes in the following directory
            watcher.Path=Dts.Variables["SemFilePath_dev"].Value.ToString();

            /* Watch for changes in  LastWrite times in the  directory. */
            watcher.NotifyFilter = NotifyFilters.LastWrite;

          //  Only watch for the following filename pattern
            watcher.Filter = "CDPreBatchEnded*";

            // Add event handlers.
            watcher.Created += new FileSystemEventHandler(OnChanged);
                // Begin watching.
        watcher.EnableRaisingEvents = true;
    }   
            // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is  created, 
        // File.Create(e.Replace("CDPreBatchEnded", "ODSExtractStarted"));
         MessageBox.Show("Hello, File exists");
    }

        }
    }

Open in new window

0
 

Author Comment

by:blossompark
ID: 39873015
ok, maybe this is where i need to look at
Using the FileSystemWatcher.WaitForChanged (WatcherChangeTypes, Int32) method you can specify how long the watcher needs to "watch".

as specified by valentino in the original comment
0
 
LVL 37

Assisted Solution

by:ValentinoV
ValentinoV earned 375 total points
ID: 39873051
The reason for the read squiggly lines is because your curly braces don't match.  Looking at the code snippet in #a39872842 you'd have to remove the opening brace on line 24 to fix this.
0
 

Author Comment

by:blossompark
ID: 39873151
Yes, I found that issue, thanks.

The code is now parsing,
but it is not doing anything, the Task executes successfully but the message Box in the event handler is not displaying.

I have a file named  CDPreBatchEndedTEST in the monitored folder.
My expectation is that the code runs, sees the file and runs the event handler to display the Message Box.
Is my expectation correct?

The current code is
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;

namespace ST_07bc0354bae645e8bf2e65be651aeec0.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion




        public void Main()
        {
            
            // MessageBox.Show("Hello, File exists");
            //          {filesystemwatcher

            //    Run();

            //    }

            //public static void Run()


//            FileSystemWatcher watcher;
//        }
//            private void watch()
//{

            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();

            //Watch for changes in the following directory
            watcher.Path=Dts.Variables["SemFilePath_dev"].Value.ToString();

            /* Watch for changes in  LastWrite times in the  directory. */
            watcher.NotifyFilter = NotifyFilters.LastWrite;

          //  Only watch for the following filename pattern
            watcher.Filter = "CDPreBatchEnded*";

            // Add event handlers.
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Changed += new FileSystemEventHandler(OnChanged);


                // Begin watching.
        watcher.EnableRaisingEvents = true;
   
    }
       
            // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is  created, 
        // File.Create(e.Replace("CDPreBatchEnded", "ODSExtractStarted"));
         MessageBox.Show("Hello, File exists");
    }

        }
    }

Open in new window

0
 
LVL 37

Assisted Solution

by:ValentinoV
ValentinoV earned 375 total points
ID: 39873244
You've created the watcher but not started it.  Try calling WaitForChanged after setting the EnableRaisingEvents property.
0
 

Author Comment

by:blossompark
ID: 39873510
Hi,
I have added WaitForChanged
and the task pauses for the time period  given to the method.
But the event handler does not seem to be called, as no Message Box is displayed.
I have tried numerous filter values and  deleted, added files to the target folder while the task is running but still no Message Box output
 public void Main()
        {
             
            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();

            //Watch for changes in the following directory
            watcher.Path=Dts.Variables["SemFilePath_dev"].Value.ToString();

            /* Watch for changes in  LastWrite times in the  directory. */
            watcher.NotifyFilter = NotifyFilters.LastWrite;

          //  Only watch for the following filename pattern
            watcher.Filter = "CDPreBatchEnded*";
           // watcher.Filter = "*.txt";

            // Add event handlers.
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Changed += new FileSystemEventHandler(OnChanged);
  
                // Begin watching.
        watcher.EnableRaisingEvents = true;
        watcher.WaitForChanged(WatcherChangeTypes.All, 20000);
       
    }
       
            // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is  created, 
        // File.Create(e.Replace("CDPreBatchEnded", "ODSExtractStarted"));
         MessageBox.Show("Hello, File exists");
    }

        }
    }

Open in new window

0
 

Author Closing Comment

by:blossompark
ID: 39909277
Hi by: ValentinoV and ryanmccauley..
thank's for the help with this, I have learnt a lot.
I havnt resolved the last  issue but will open a new question if I am still  unable to resolve.
Thanks again
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 60
SQL USE DATABASE VARIABLE 5 29
SQL Group By Question 4 20
SQL Recursion 6 20
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question