SSIS脚本任务 - VB循环问题

时间:2018-04-27 14:59:05

标签: vb.net ssis

SSIS中的以下脚本任务连接到FTP服务器,并且应该在文件存在之前查找该文件,然后将该文件复制到本地文件夹。它正确地完成所有操作,但不是查找特定文件,而是复制所有文件。

由于我不是VB作家,因此我将各种论坛拼凑在一起。似乎忽略了fileName.Contains。

任何帮助都会很棒。谢谢!

' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic 2008.
' The ScriptMain is the entry point class of the script.

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

<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute>
<System.CLSCompliantAttribute(False)>
Partial Public Class ScriptMain
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

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


    Public Sub Main()
        System.Threading.Thread.Sleep(50000)

        Dim VarCol As Variables = Nothing

        Dts.VariableDispenser.LockForWrite("User::FileFound")
        Dts.VariableDispenser.LockForWrite("User::FileName")


        Dts.VariableDispenser.GetVariables(VarCol)


        Try
            'Create the connection to the ftp server
            Dim cm As ConnectionManager = Dts.Connections.Add("FTP")
            Dim strFolders As String()
            Dim strFiles As String()
            Dim fileCount As Int32

            fileCount = 0

            Dim fileName As String

            'Set the properties like username & password
            cm.Properties("ServerName").SetValue(cm, "ftp.testing.com")
            cm.Properties("ServerUserName").SetValue(cm, "username")  'user name
            cm.Properties("ServerPassword").SetValue(cm, "password") 'password
            Dim ftp As FtpClientConnection = New FtpClientConnection(cm.AcquireConnection(Nothing))

            'Connects to the ftp server
            ftp.Connect()
            ftp.SetWorkingDirectory("/testing")
            ftp.GetListing(strFolders, strFiles)

            For Each fileName In strFiles

                If fileName.Contains("test.xml") Then 'file has such word in its name

                    ftp.ReceiveFiles(strFiles, "\\FTPSERVER\c$\FTP FILES\testing", True, False)  'download file if found

                    fileCount = fileCount + 1

                    VarCol("User::FileFound").Value = fileName
                    VarCol("User::FileFound").Value = True

                Else

                    VarCol("User::FileFound").Value = False

                End If

            Next
            ftp.Close()
            VarCol.Unlock()
        Catch ex As Exception
            Dts.TaskResult = ScriptResults.Failure
        End Try
        Dts.TaskResult = ScriptResults.Success
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

好吧,我不熟悉VB代码,所以语法很可能不正确,但这里的逻辑应该做你需要的(你只需要更新以匹配VB语法)。我在代码中添加了注释以显示我正在做的事情以及在当前逻辑中返回其中一个文件名的问题(我没有修复,我只是指出了它)。

//declare string array to pass to your FTP call for only matching fiels
Dim FileNameListMatching as String() 

For Each fileName In strFiles

                If fileName.Contains("test.xml") Then 'file has such word in its name
                    // then if the file name matches in the if above, add that filename at the fileCount location into the string array
                    FileNameListMatching(fileCount) = fileName


                    fileCount = fileCount + 1

                    // this will have a problem here though because you are populating a variable with the fileName, but if there is more then 1 fileName found in your logic, it will overwrite it with only the most recent file name.  The True value is fine, because you dont care if there is more then 1 for that, but the file name returned will only give you the most recent file name if more then 1
                    VarCol("User::FileFound").Value = fileName
                    VarCol("User::FileFound").Value = True

                Else

                    VarCol("User::FileFound").Value = False

                End If

            Next

    -- then if filecount is > 0 then call your FTP to copy files
    if fileCount > 0
        ftp.ReceiveFiles(FileNameListMatching, "\\FTPSERVER\c$\FTP FILES\testing", True, False)  'download file if found