Pages

Tuesday, March 9, 2010

Checking for the file?


Question:
Many times I observed on MSDN SSIS forum that developers are facing problem for checking the file in a particular folder. There are 2 types of checking:

1> Is any file exist in the folder?
OR
Count number of files in a directory through SSIS

2> A particular file is exist [for example c:\Manish\demo.txt]

Answer:

1> Is any file exist in the folder? OR Count number of files in a directory through SSIS?


For checking the file existence in a directory, there are 2 ways

1: File Property task
File Properties Task .  It will detect file existence. Check the link.

2: Script task

Step1: Take a ForEachLoop container.
Step2: Map to the particular directory.

Step3: Inside that you have to use a script task and there you can write following code to count the no of files.

Step4: Define the following variable @ package level 
 
User::Count

 
---------------------------
|   ForEachLoop             |
---------------------------
|
|            --------------
|            |Script Task |
|            --------------
|
---------------------------

Step5:

Public Sub Main()
    Dts.Variables("User::Count").Value = Dts.Variables("User::Count").Value + 1
    MsgBox(Dts.Variables("User::Count").Value.ToString())
    Dts.TaskResult = ScriptResults.Success
End Sub
    
Now you can access the count variable



2> A particular file is exist [for example c:\Manish\demo.txt]

You can use a script task to check if the file exists or not. Suppose I have demo.txt in C:\Manish then I will use a script task with code as:
 
Public Class ScriptMain
    Dim flag As Boolean
    Public Sub Main()
        flag = File.Exists("C:\MyFolder\demo.txt")
        If flag = True Then
            System.Windows.Forms.MessageBox.Show("File Exists")
        End If
    Dts.TaskResult = Dts.Results.Success
    End Sub
End Clas



Hope this will help you. Give your suggestion to improve this post.

No comments:

Post a Comment