.net code using FTP

Advertisement

hamzajosh
Joined:
Posts:
1
Location:
Chicago

.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

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: .net code using FTP

hamzajosh wrote:

1) It does not get directory list for /export/A*.zip, it only gets back listing for /export/.
WinSCP .NET assembly does not support file mask with Session.ListDirectory. You need to filter the results on your own.

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 attach a full session log file showing the problem (using the latest version of WinSCP).

To generate log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you may email it to me. You will find my address (if you log in) in my forum profile. Please include link back to this topic in your email. Also note in this topic that you have emailed the log.

Reply with quote

Advertisement

You can post new topics in this forum