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
64 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
Comment Utility
I am currently experimenting with a For Loop Container
0
 
LVL 37

Accepted Solution

by:
ValentinoV earned 375 total points
Comment Utility
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
Comment Utility
Hi ValentinoV
Thanks for your response, will study this and update later
0
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
deleting curly bracket , now  only obvious issue is with 'Replace' in
    File.Create(e.Replace("CDPreBatchEnded", "ODSExtractStarted"));

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:blossompark
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You've created the watcher but not started it.  Try calling WaitForChanged after setting the EnableRaisingEvents property.
0
 

Author Comment

by:blossompark
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Create XML 5 30
Row-Level Security 2 15
Job Dependencies in SQL Server Agent: 1 12
Sql query to Stored Procedure 6 13
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now