Search code examples
sql-servervb.netssisetlssis-2012

SSIS Script Task - how accept wildcard files in VB.net


I have this SSIS script task code copied from a site. I'm trying to avoid FTP failed error when the folder is empty. Does not need to a specific name file. The code below is for specific filename. How to make the filename a wildcard?

Public Sub Main()

    Dim StrFolderArrary As String()
    Dim StrFileArray As String()
    Dim fileName As String
    Dim RemoteDirectory As String


    RemoteDirectory = Dts.Variables("User::RemoteFolder").Value.ToString()

    Dim cm As ConnectionManager = Dts.Connections("FTPConnection") 'FTP connection manager name
    Dim ftp As FtpClientConnection = New FtpClientConnection(cm.AcquireConnection(Nothing))

    ftp.Connect() 'Connecting to FTP Server


    ftp.SetWorkingDirectory(RemoteDirectory) 'Provide the Directory on which you are working on FTP Server

    ftp.GetListing(StrFolderArrary, StrFileArray) 'Get all the files and Folders List

    'If there is no file in the folder, strFile Arry will contain nothing, so close the connection.

    If StrFileArray Is Nothing Then

        MessageBox.Show(Dts.Variables("User::Flag").Value.ToString())
        ftp.Close()
        Dts.Variables("User::Flag").Value = 0


        'If Files are there, Loop through the StrFileArray arrary and insert into table

    Else



        For Each fileName In StrFileArray

            MessageBox.Show(fileName)
            If fileName = Dts.Variables("User::FileName").Value.ToString() Then
                Dts.Variables("User::Flag").Value = 1
                MessageBox.Show(Dts.Variables("User::Flag").Value.ToString())
            End If
        Next

        ftp.Close()

    End If
    ' Add your code here
    '
    Dts.TaskResult = ScriptResults.Success
End Sub

Solution

  • first it is better to add the following Validation to the If statment:

    If StrFileArray Is Nothing OrElse _ 
       StrFileArray.length = 0 Then
    

    Filter using Linq (need to import System.Linq)

    If StrFileArray.Where(Function(x) x.equals(Dts.Variables("User::FileName").Value)).ToList().Count() > 0 Then
    
        Dts.Variables("User::Flag").Value = 1
    
    End If
    

    UPDATE `

    After reading your comment

    If StrFileArray.Where(Function(x) x.StartsWith("Abc") AndAlso x.EndsWith(".txt")).ToList().Count() > 0 Then
    
        Dts.Variables("User::Flag").Value = 1
    
    End If