[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 74
  • Last Modified:

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

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
blossompark
Asked:
blossompark
  • 10
  • 3
4 Solutions
 
blossomparkAuthor Commented:
I am currently experimenting with a For Loop Container
0
 
ValentinoVBI ConsultantCommented:
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
 
blossomparkAuthor Commented:
Hi ValentinoV
Thanks for your response, will study this and update later
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ryan McCauleyCommented:
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
 
blossomparkAuthor Commented:
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
 
blossomparkAuthor Commented:
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
 
blossomparkAuthor Commented:
deleting curly bracket , now  only obvious issue is with 'Replace' in
    File.Create(e.Replace("CDPreBatchEnded", "ODSExtractStarted"));

Open in new window

0
 
blossomparkAuthor Commented:
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
 
blossomparkAuthor Commented:
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
 
ValentinoVBI ConsultantCommented:
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
 
blossomparkAuthor Commented:
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
 
ValentinoVBI ConsultantCommented:
You've created the watcher but not started it.  Try calling WaitForChanged after setting the EnableRaisingEvents property.
0
 
blossomparkAuthor Commented:
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
 
blossomparkAuthor Commented:
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

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!

  • 10
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now