SSIS 脚本任务 - 如何在 VB.net 中接受通配符文件

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

我从站点复制了这个 SSIS 脚本任务代码。当文件夹为空时,我试图避免 FTP failed 错误。不需要特定名称的文件。下面的代码是针对特定文件名的。如何使文件名成为通配符?

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

首先最好在 If 语句中添加以下验证:

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

使用 Linq 过滤(需要导入 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

更新`

阅读您的评论后

If StrFileArray.Where(Function(x) x.StartsWith("Abc") AndAlso x.EndsWith(".txt")).ToList().Count() > 0 Then

    Dts.Variables("User::Flag").Value = 1

End If