Vb.net SSIS脚本任务-VB循环问题

Vb.net SSIS脚本任务-VB循环问题,vb.net,ssis,Vb.net,Ssis,SSIS中的以下脚本任务连接到FTP服务器,应该查找文件,直到它存在,然后将该文件复制到本地文件夹。它做的每件事都是正确的,但它不是在寻找特定的文件,而是在复制所有的文件 由于我不是VB的作者,所以我从不同的论坛上拼凑了这个脚本。似乎忽略了文件名.Contains 任何帮助都会很好。谢谢 ' Microsoft SQL Server Integration Services Script Task ' Write scripts using Microsoft Visual Basic 2008

SSIS中的以下脚本任务连接到FTP服务器,应该查找文件,直到它存在,然后将该文件复制到本地文件夹。它做的每件事都是正确的,但它不是在寻找特定的文件,而是在复制所有的文件

由于我不是VB的作者,所以我从不同的论坛上拼凑了这个脚本。似乎忽略了文件名.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
Microsoft SQL Server集成服务脚本任务 '使用Microsoft Visual Basic 2008编写脚本。 'ScriptMain是脚本的入口点类。 导入系统 导入系统数据 导入系统。数学 导入Microsoft.SqlServer.Dts.Runtime 部分公共类ScriptMain 继承Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase 枚举脚本结果 Success=Microsoft.SqlServer.Dts.Runtime.dtsesecresult.Success Failure=Microsoft.SqlServer.Dts.Runtime.dtsesecresult.Failure 结束枚举 公用分干管() 系统线程线程睡眠(50000) Dim VarCol作为变量=无 Dts.VariableDispenser.LockForWrite(“用户::文件已找到”) Dts.variablepender.LockForWrite(“用户::文件名”) Dts.VariableDispenser.GetVariables(VarCol) 尝试 '创建到ftp服务器的连接 Dim cm As ConnectionManager=Dts.Connections.Add(“FTP”) 将strFolders设置为字符串()的尺寸 将strFiles设置为字符串() 将文件计数设置为Int32 fileCount=0 将文件名设置为字符串 '设置用户名和密码等属性 cm.Properties(“ServerName”).SetValue(cm,“ftp.testing.com”) cm.Properties(“ServerUserName”).SetValue(cm,“username”)”用户名 cm.Properties(“ServerPassword”).SetValue(cm,“password”)”密码 Dim ftp As FtpClientConnection=新的FtpClientConnection(cm.AcquireConnection(Nothing)) '连接到ftp服务器 ftp.Connect() ftp.SetWorkingDirectory(“/testing”) ftp.GetListing(strFolders、strFiles) 对于strFiles中的每个文件名 如果fileName.Contains(“test.xml”),则“文件名中有这样的词 ftp.ReceiveFiles(strFiles,“\\FTPSERVER\c$\ftp FILES\testing”,True,False)”下载文件(如果找到) fileCount=fileCount+1 VarCol(“User::FileFound”).Value=fileName VarCol(“User::FileFound”).Value=True 其他的 VarCol(“User::FileFound”).Value=False 如果结束 下一个 ftp.Close() VarCol.Unlock() 特例 Dts.TaskResult=ScriptResults.Failure 结束尝试 Dts.TaskResult=ScriptResults.Success 端接头 末级
好的,我不熟悉VB代码,所以语法很可能不正确,但这里的逻辑应该满足您的需要(您只需更新以匹配VB语法)。我在代码中添加了注释,以显示我正在做什么,并且在当前逻辑中返回一个文件名时出现了问题(我没有修复,我只是指出了它)


在ftp.ReceiveFiles调用中,您正在传递它的strFiles,这是所有文件的列表,我想您可能只想传递fileName变量?如果我将strFiles更改为fileName,我会得到以下错误:“String”无法转换为“String()”好的,ftp.ReceiveFiles需要一个a String(),fileName只是一个字符串。您可以构建与列表匹配的新文件数组。因此,在fileName.contains if语句中,将所有文件名添加到String()数组中,然后在循环遍历所有文件名后,仅使用在新字符串数组中填充的匹配文件调用ftp.ReceiveFiles。这是有道理的,但不确定如何写入…我尝试了几次迭代,但是我的最终结果并没有返回任何东西。并没有返回任何东西?什么意思?你能一步一步地检查代码,看看哪里有问题吗?设置的值是什么,在哪里?在调整代码之前,我会将FTP目录中的所有文件检索到我的本地目录。根据您的规范调整代码后,脚本将成功编译,但不会检索任何文件。如果您首先从FTP站点检索所有文件,则上述代码将不会执行任何操作,因为它已从FTP站点提取所有文件,因此无需执行任何操作?因此,如果在从FTP站点拉取所有文件名后循环遍历这些文件名,则文件列表将为空,因为FTP站点上不再有文件。所以循环和if语句将永远不会被命中,因为循环将没有任何东西可以循环通过?您是否尝试过在调试过程中单步进入代码并查看哪些变量中的值?
//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