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
70 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

A couple of weeks ago, my client requested me to implement a SSIS package that allows them to download their files from a FTP server and archives them. Microsoft SSIS is the powerful tool which allows us to proceed multiple files at same time even w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

726 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