Programmatically Checking If File Exits In SSIS

In  ‘Generating Files through bcp Utility in SQL Server’ post I showed how to create a simple CSV file and output it onto a file system. In this post I will demonstrate how to programmatically check if the file exists in the directory where we intended to place it (it enables us to take action if the file has not been created as expected). This can be incorporated into a process of creating files via bcp utility as its extension; for example, if the file has not been created notify operator(s) or perform some other action.

Firstly, we will create a variable named FileExists (you can call it anything you want as long as you stick to the name throughout the process) of Boolean type, set the scope to ‘Package’ and set its value to FALSE. This variable will store our result and we will use it in a VB.NET script as well as precedence constraints.

Next, ensure that our FileExists variable has been declared in the ‘Script Task Editor’ for ‘ReadOnlyVariables’ field as per below and that the language selected is set to Microsoft Visual Basic 2008.

We are now ready to create a simple VB.NET script which populates the FileExists variable (setting to TRUE or FALSE) depending on whether the file that we wish to check for existence  is there or not.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO

Public Class ScriptMain

    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum

    Partial Public Class ScriptMain
        Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

        Public Sub Main()

            Dim directory As DirectoryInfo = New DirectoryInfo("c:\")
            Dim file As FileInfo() = directory.GetFiles("*.csv")

            If file.Length > 0 Then
                Dts.Variables("User::FileExists").Value = True
            Else
                Dts.Variables("User::FileExists").Value = False
            End If

            Dts.TaskResult = ScriptResults.Success
         End Sub
    End Class
End Class

All the above script does is checking if the file exists on the c:\ drive and then populating our FileExists SSIS package variable with either a value of ‘TRUE’ if it does and ‘FALSE’ if it doesn’t. This can then be used by controlling the flow of the subsequent package containers based on the logic applied in the precedence constraint which follows the script task. In order to redirect the flow, let’s create two dummy ‘Execute SQL Task’ containers, link them to our ‘Script Task’ and populate them with some dummy SQL code (two dashes i.e. — will do for this exercise). Next, we will apply a simple logic in the expression field of the two ‘Precedence Constraints’of to how to proceed with the execution. In the first one, set the ‘Evaluation Operation’ to Expression and type in the following expression in the ‘Expression’ field: @FileExists==TRUE as per below.

The second ‘Precedence Constraint’ will look almost identical with the exception of the actual expression. Here, we change it to @FileExists==FALSE. When finished, you should see two blue squares next to our ‘Precedence Constraints’ and the actual connections should change their color to blue as well (SQL Server 2008 R2). So far, the part of the package we have just developed should look like this.

To check if the package works, all we need to do is to put a CSV file in the target directory (c:\ drive in our case) and the execution should follow the path of the ‘Precedence Constraint’ where the expression was set to ‘@FileExists==TRUE’. If we delete the file, the execution should go with the other Precendence Constraint. Those two scenarios are pictured below.

This can be further developed by incorporating other tasks and actions e.g. if @FileExists==FALSE (meaning that no CSV file was found), ‘Send Mail Task’ can be run to notify operator(s).

To check if the package works, all we need to do is to put a CSV file in the target directory (c:\ drive in our case) and the execution should follow the path of the ‘Precedence Constraint’ where the expression was set to ‘@FileExists==TRUE’. If we delete the file, the execution should go with the other Precendence Constraint. Those two scenarios are pictured above.

http://scuttle.org/bookmarks.php/pass?action=add

Tags: , , , , ,

This entry was posted on Tuesday, April 17th, 2012 at 8:19 am and is filed under .NET, SQL, SSIS. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 Responses to “Programmatically Checking If File Exits In SSIS”

Oded Dror October 5th, 2013 at 4:58 am

Hi there,
I’m using VS 2012 and I follow yours instruction and it work when the file Exist
When the file is not I’m getting an error
Error at Get Excel Data [Excel Source [2]]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E37.

Error at Get Excel Data [Excel Source [2]]: Opening a rowset for “Sched_data$” failed. Check that the object exists in the database.

Error at Get Excel Data [SSIS.Pipeline]: “Excel Source” failed validation and returned validation status “VS_ISBROKEN”.

Error at Get Excel Data [SSIS.Pipeline]: One or more component failed validation.

Error at Get Excel Data: There were errors during task validation.

(Microsoft.DataTransformationServices.VsIntegration)

Thanks
Oded Dror

Oded Dror October 5th, 2013 at 3:03 pm

Got fix,
I did I make sure that Delay Validation = False and it works!

Thanks,
Oded Dror

Adi March 3rd, 2014 at 6:40 am

Thanks man worked like a charm.

Sarah May 9th, 2014 at 9:27 pm

What if the directory is a user variable? How do I reference it?

DirectoryInfo needs a string.

I tried:

Dim di As DirectoryInfo = New DirectoryInfo (“User::RootPath”)

I get the error that it cannot be converted to a string.

admin May 10th, 2014 at 12:58 am

Did you initially declare your User::RootPath variable as a string? You shouldn’t have to convert it implicitly into a string as long as the initial definition was using a string data type……..cheers, Marcin

Saeed Karimi October 1st, 2015 at 10:54 pm

Thank you. It works great. Best example of seen so far

Regards
Saeed

Leave a Reply