从FTP列出/下载/删除文件 [英] list/download/delete file from FTP

查看:110
本文介绍了从FTP列出/下载/删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试生成一个脚本,该脚本可以处理来自FTP的多个文件.
例如,我在FTP上有4个文件,我需要将它们保存在本地,然后将其从FTP中删除.

我正在尝试列出和获取的示例,但它无法正常工作.

这是我所拥有的:

Hi,

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:

# 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



上面的函数列出了文件夹的内容,并调用下面的函数下载文件:



above function list the content of the folder and calls below function which downloads the files:

<pre lang="vb"># 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





我有4个档案;对于其中3个来说,它工作正常,但最后一个应用程序崩溃了.





I have 4 files; it is working OK for 3 of them but the application crashes on the last one.

Can someone help me, please?

推荐答案

正如曼弗雷德(Manfred)所说,您没有给予太多帮助.

但是查看您的代码

As Manfred put you haven''t given much to go on.

BUT looking at your code

Dim buffer(2047) As Byte

将缓冲区大小设置为2047,我将检查文件之间的大小差异.

如果即时消息不正常,请扩展错误消息的类型,发生的位置等,这将帮助我们更好地建议您

c#示例,但易于转换为vb .net [ ^ ]

you set the buffer size to 2047, I would check what the size differences are between the files.

If im off the mark please expand on the type of error message, where its happening etc as that will help us advise you better

c# example but easy to convert to vb.net[^]


My.Computer.Network.UploadFile("d:\reza.txt", "ftp://www.aaaaa.ir/reza.txt", "user name", " Password")


这篇关于从FTP列出/下载/删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆