M. Mehta
M. Mehta

Reputation: 73

SSIS - Script Task Check if Subfolder Exists

I want to check if sub-folder exists or not. If exists, move on. If not exists go to next task.

My sub-folder is "C:\Folder1\Folder2\Folder3" I want to check if Folder3 is exists or not.

I worked on it. Create 2 variables

1> FolderPath = C:\Folder1\Folder2

2> FolderExists = Boolean = False

Script Task ReadOnlyVariable = @FolderPAth ReadWriteVariable = @FolderExists

Following script I add in edit script

    Dim DirExists As String

    DirExists = Dir(CStr(Dts.Variables("Folder3").Value))

    If DirExists <> "" Then

        Dts.Variables("Folder3").Value = True

    Else

        Dts.Variables("Folder3").Value = False

    End If

Can some one correct me please.

Upvotes: 4

Views: 6788

Answers (1)

Matt
Matt

Reputation: 14341

Based on your comment it doesn't seem like you will care if it is c# of VB so here are steps from beginning to end on how to test Existence of a folder and use it in constrained precedence.

  • Define 2 Package Level Variables: FolderPath string, FolderExists boolean enter image description here
  • Add Script Task and configure for C# and add FolderPath as a ReadOnlyVariable and FolderExists as a ReadWriteVariable enter image description here
  • Click Edit to Edit the script
  • Scroll to "#region Namespaces" near the top and add using System.IO; enter image description here
  • Scroll to the definition of the Main() sub and add the first line after "TODO" below so that the routine becomes:

    public void Main()
    {
        // TODO: Add your code here
        Dts.Variables["User::FolderExists"].Value = Directory.Exists(Dts.Variables["User::FolderPath"].Value.ToString());
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    
  • The script task is complete and you should now be able to use the FolderExists variable as the expression for constrained precedence.

  • Add your next step in the package and connect with the green success arrow then double click the arrow and set the Constraint Options to evaluate for Expression and Constraint and the expression simply as the FolderExists variable. enter image description here

this solution was fully tested and is operational

Upvotes: 3

Related Questions