Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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




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?
Posted
Updated 31-Mar-11 5:41am
Comments
Manfred Rudolf Bihy 22-Mar-11 8:30am    
What exception is thrown in what function and line?
A little bit more detail would indeed be helpful in us assisting you.
Dalek Dave 31-Mar-11 11:41am    
Edited for Grammar.

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

BUT looking at your code

Dim buffer(2047) As Byte
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[^]
 
Share this answer
 
v2
Comments
mikcutu 22-Mar-11 11:46am    
hi, my files are very small (1 K - each of them).
it seems that my "str" variable become nothing right before last loop.

here is the error message which occur at:
str1 = str.Substring(InStr(str, "/"), str.Length - InStr(str, "/"))--> [b]Object reference not set to an instance of an object.[/b]

thanks for any comment
Simon_Whale 23-Mar-11 5:02am    
have you checked to see if str has any values before it gives you an error?
mikcutu 24-Mar-11 3:36am    
Hi Simon,

my test is made with 4 files, 3 of them are downloaded correctly but during the extraction of last one, the value of str becomes nothig...

what should I do?
Simon_Whale 24-Mar-11 4:41am    
you need to check variable earlier on in your code to see where the response become null / nothing. Me personally I would check response to see if that has values on the 4th file download
mikcutu 5-Apr-11 8:03am    
Hi Simon,

I was review my code and it seems that the problem comes from the passing over the 1st value of the file name. More exactly:

1st of all, I have this declaration:
Dim str As String = sr.ReadLine
here, immediately the str variable is declared, it is initialized with the 1st filename.

later, I enter into the "While str IsNot Nothing" where i have a new initialization of "str" variable without using the previous value. I am thinking to use 1st value of "str" with GetFile procedure and after that to have the while loop...

what do you say about this, could be a good solution...?
...any other clue?

Thanks a lot.
hi
My.Computer.Network.UploadFile("d:\reza.txt", "ftp://www.aaaaa.ir/reza.txt", "user name", " Password")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900