.net code using FTP
I had code that I was using to connect to ftp server using SFTP and it works fine. Now I have a new server i need to connect to that is FTP. I modified the code and it gives me errors which are:
1) It does not get directory list for /export/A*.zip, it only gets back listing for /export/.
2) It does not copy the file from FTP to Local directory. TransferResult.check() gives me error "Can't get attributes of file '/export/XYZ.zip'."
The inner exception says "'/export/XYZ.zip' does not exist "
Please HELP, been struggling too long with this.
Public Sub Main()
Dim LocalFilePath As String, ArchivePath As String, ArchiveFilePath As String, RetryCount As Integer
' Setup session options
Dim sessionOptions As New SessionOptions
With sessionOptions
.Protocol = Protocol.Ftp
.HostName = Dts.Variables("FTPURL").Value
.UserName = Dts.Variables("FTPUserName").Value
.Password = Dts.Variables("FTPPassword").Value
End With
Dim ExeName As String = ""
ExeName = Dts.Variables("WinSCPExePath").Value
Dim session As Session = New Session
Using session
session.ExecutablePath = ExeName
RetryCount = 1
Do
Try
session.Open(sessionOptions)
Exit Do
Catch
Thread.Sleep(30000)
RetryCount += 1
If RetryCount > 10 Then
Dts.TaskResult = ScriptResults.Failure
Exit Sub
End If
End Try
Loop
' Download files
Dim transferOptions As New TransferOptions
transferOptions.TransferMode = TransferMode.Binary
LocalFilePath = Dts.Variables("SourcePathName").Value
ArchivePath = Replace(LocalFilePath, "ETL\FTPInbound", "ETL\Archive\FTPInbound")
Dim transferResult As TransferOperationResult
Dim remoteDirectory As String
Dim directory As RemoteDirectoryInfo
Dim remoteFileName As String
Dim remoteFilePath As String
Dim Command As String
directory = session.ListDirectory("/export/")
For Each fileInfo In directory.Files
remoteFileName = fileInfo.Name
If Right(remoteFileName, 4) = ".zip" And Left(remoteFileName, 3) = "XYZ" Then
remoteFilePath = "/" + remoteDirectory + "/" + remoteFileName
ArchiveFilePath = ArchivePath + remoteFileName
If Not (My.Computer.FileSystem.FileExists(ArchiveFilePath)) Then
transferResult = session.GetFiles(remoteFilePath, LocalFilePath, False, transferOptions)
transferResult.Check()
End If
End If
Next
End Using
Dts.TaskResult = ScriptResults.Success
End Sub
1) It does not get directory list for /export/A*.zip, it only gets back listing for /export/.
2) It does not copy the file from FTP to Local directory. TransferResult.check() gives me error "Can't get attributes of file '/export/XYZ.zip'."
The inner exception says "'/export/XYZ.zip' does not exist "
Please HELP, been struggling too long with this.
Public Sub Main()
Dim LocalFilePath As String, ArchivePath As String, ArchiveFilePath As String, RetryCount As Integer
' Setup session options
Dim sessionOptions As New SessionOptions
With sessionOptions
.Protocol = Protocol.Ftp
.HostName = Dts.Variables("FTPURL").Value
.UserName = Dts.Variables("FTPUserName").Value
.Password = Dts.Variables("FTPPassword").Value
End With
Dim ExeName As String = ""
ExeName = Dts.Variables("WinSCPExePath").Value
Dim session As Session = New Session
Using session
session.ExecutablePath = ExeName
RetryCount = 1
Do
Try
session.Open(sessionOptions)
Exit Do
Catch
Thread.Sleep(30000)
RetryCount += 1
If RetryCount > 10 Then
Dts.TaskResult = ScriptResults.Failure
Exit Sub
End If
End Try
Loop
' Download files
Dim transferOptions As New TransferOptions
transferOptions.TransferMode = TransferMode.Binary
LocalFilePath = Dts.Variables("SourcePathName").Value
ArchivePath = Replace(LocalFilePath, "ETL\FTPInbound", "ETL\Archive\FTPInbound")
Dim transferResult As TransferOperationResult
Dim remoteDirectory As String
Dim directory As RemoteDirectoryInfo
Dim remoteFileName As String
Dim remoteFilePath As String
Dim Command As String
directory = session.ListDirectory("/export/")
For Each fileInfo In directory.Files
remoteFileName = fileInfo.Name
If Right(remoteFileName, 4) = ".zip" And Left(remoteFileName, 3) = "XYZ" Then
remoteFilePath = "/" + remoteDirectory + "/" + remoteFileName
ArchiveFilePath = ArchivePath + remoteFileName
If Not (My.Computer.FileSystem.FileExists(ArchiveFilePath)) Then
transferResult = session.GetFiles(remoteFilePath, LocalFilePath, False, transferOptions)
transferResult.Check()
End If
End If
Next
End Using
Dts.TaskResult = ScriptResults.Success
End Sub