I am trying to generate a script which handles more than 1 file from FTP.
For example I have 4 files on the FTP and I need to have them saved locally and after that to delete them from FTP.
I was trying and example for list and get but it is not working correctly.
here is what i have:
Expand|Select|Wrap|Line Numbers
- Public Function GetFileAndFileList(ByVal StartsWith As String, ByVal EndsWith As String) As List(Of String)
- Dim oFTP As FtpWebRequest = CType(FtpWebRequest.Create("ftp://testftp.host-ed.net/customer"), FtpWebRequest)
- oFTP.Credentials = New NetworkCredential("xxx", "yyy")
- oFTP.KeepAlive = False
- 'oFTP.EnableSsl = UseSSL
- 'If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
- oFTP.Method = WebRequestMethods.Ftp.ListDirectory
- Dim response As FtpWebResponse = CType(oFTP.GetResponse, FtpWebResponse)
- Dim sr As StreamReader = New StreamReader(response.GetResponseStream)
- Dim str As String = sr.ReadLine
- Dim oList As New List(Of String)
- Dim str1 As String 'declare an array of string to keep the list of file names
- Dim i, n As Integer 'declare variables to loop into the array
- n = 1
- While str IsNot Nothing
- If str.StartsWith(StartsWith) And str.EndsWith(EndsWith) Then
- oList.Add(str.Substring(InStr(str, "/"), str.Length - InStr(str, "/")))
- str = sr.ReadLine
- str1 = str.Substring(InStr(str, "/"), str.Length - InStr(str, "/"))
- GetFile(str1, "C:\XXX")
- n = n + 1
- End If
- End While
- sr.Close()
- response.Close()
- oFTP = Nothing
- MsgBox(oList.Item(0))
- Return oList
- End Function
Expand|Select|Wrap|Line Numbers
- Public Function GetFile(ByVal Name As String, ByVal DestFile As String) As Boolean
- Dim oFTP As FtpWebRequest = CType(FtpWebRequest.Create("ftp://testftp.host-ed.net/customer/" & Name), FtpWebRequest)
- oFTP.Credentials = New NetworkCredential("xxx", "yyy")
- oFTP.Method = WebRequestMethods.Ftp.DownloadFile
- oFTP.KeepAlive = False
- 'oFTP.EnableSsl = UseSSL
- 'If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
- oFTP.UseBinary = True
- Dim response As FtpWebResponse = CType(oFTP.GetResponse, FtpWebResponse)
- Dim responseStream As Stream = response.GetResponseStream
- Dim fs As New FileStream(DestFile & "\" & Name, FileMode.Create)
- Dim buffer(2047) As Byte
- Dim read As Integer = 1
- While read <> 0
- read = responseStream.Read(buffer, 0, buffer.Length)
- fs.Write(buffer, 0, read)
- End While
- responseStream.Close()
- fs.Flush()
- fs.Close()
- responseStream.Close()
- response.Close()
- oFTP = Nothing
- Return True
- End Function
Can someone help me, please?