Need help on following VB Code

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am using following code to send files to FTP server, but if there are no files in FTP server it is failing how to bypass and send files to FTp server.
Code will compare the files between Local and Ftp files, files which are not in FTpserver will be copied over but it fails when there are no files in FTp server.
Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
Public Class ScriptMain
Public Sub Main()
Try
Dim ftp_fileList As String()
Dim ftp_folderList As String()
Dim local_fileList As String()
Dim getFiles As String()
Dim LocalPaths As String = Dts.Variables("User::LocalPaths").Value.ToString()
Dim ftpPaths As String = Dts.Variables("User::FtpPaths").Value.ToString()
Dim filelist As String() = Directory.GetFiles(Path.GetDirectoryName(LocalPaths))
Dim cm As ConnectionManager = Dts.Connections.Add("FTP")
Set the properties like username & password
cm.Properties("ServerName").SetValue(cm, "xxxxx")
cm.Properties("ServerUserName").SetValue(cm, "xxx")
cm.Properties("ServerPassword").SetValue(cm, "xxxx")
cm.Properties("ServerPort").SetValue(cm, "21")
cm.Properties("Timeout").SetValue(cm, "0") The 0 setting will make it not timeout
cm.Properties("ChunkSize").SetValue(cm, "1000") 1000 kb
cm.Properties("Retries").SetValue(cm, "1")
create the FTP object that sends the files and pass it the connection created above.
Dim ftp As FtpClientConnection = New FtpClientConnection(cm.AcquireConnection(Nothing))

ftp.Connect()

ftp.SetWorkingDirectory(Dts.Variables("FtpPaths").Value.ToString())
Get a list of all available files on the ftp server.
ftp.GetListing(ftp_folderList, ftp_fileList)

Get a list of all existing files on the local drive.
local_fileList = Directory.GetFiles(LocalPaths)

Build a list of files which need to be downloaded.
Dim getList As New Text.StringBuilder()
For Each File As String In filelist
Dim comp As String = ftpPaths & File
If Array.IndexOf(ftp_fileList, comp) = -1 Then
getList.Append(File)
getList.Append("|")
End If
Next
If getList.Length > 0 Then
getList.Remove(getList.Length - 1, 1)
getFiles = getList.ToString().Split("|"c)

ftp.SendFiles(getFiles, ftpPaths, True, False)
End If

ftp.Close()

Dts.TaskResult = ScriptResults.Success
Catch ex As Exception

Fire error and set result to failure
Dts.Events.FireError(0, "FTP Script Task", "Error: " + ex.Message, String.Empty, 0)
Dts.Variables("User::Email").Value = "FTP Operation failed to download files " + Chr(13) + "This is a automated email. Please do not reply to this distribution email."
Dts.TaskResult = ScriptResults.Failure

End Try
End Sub
End Class
s

View the full article
 
Back
Top