473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

list/download/delete file from 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:

Expand|Select|Wrap|Line Numbers
  1. Public Function GetFileAndFileList(ByVal StartsWith As String, ByVal EndsWith As String) As List(Of String)
  2.         Dim oFTP As FtpWebRequest = CType(FtpWebRequest.Create("ftp://testftp.host-ed.net/customer"), FtpWebRequest)
  3.         oFTP.Credentials = New NetworkCredential("xxx", "yyy")
  4.         oFTP.KeepAlive = False
  5.         'oFTP.EnableSsl = UseSSL
  6.         'If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
  7.         oFTP.Method = WebRequestMethods.Ftp.ListDirectory
  8.         Dim response As FtpWebResponse = CType(oFTP.GetResponse, FtpWebResponse)
  9.         Dim sr As StreamReader = New StreamReader(response.GetResponseStream)
  10.         Dim str As String = sr.ReadLine
  11.         Dim oList As New List(Of String)
  12.  
  13.         Dim str1 As String 'declare an array of string to keep the list of file names
  14.         Dim i, n As Integer              'declare variables to loop into the array
  15.         n = 1
  16.         While str IsNot Nothing
  17.             If str.StartsWith(StartsWith) And str.EndsWith(EndsWith) Then
  18.                 oList.Add(str.Substring(InStr(str, "/"), str.Length - InStr(str, "/")))
  19.                 str = sr.ReadLine
  20.                 str1 = str.Substring(InStr(str, "/"), str.Length - InStr(str, "/"))
  21.                 GetFile(str1, "C:\XXX")
  22.                 n = n + 1
  23.             End If
  24.         End While
  25.         sr.Close()
  26.         response.Close()
  27.         oFTP = Nothing
  28.         MsgBox(oList.Item(0))
  29.         Return oList
  30.     End Function
  31.  
above function list the content of the folder and calls below function which downloads the files:

Expand|Select|Wrap|Line Numbers
  1. Public Function GetFile(ByVal Name As String, ByVal DestFile As String) As Boolean
  2.         Dim oFTP As FtpWebRequest = CType(FtpWebRequest.Create("ftp://testftp.host-ed.net/customer/" & Name), FtpWebRequest)
  3.         oFTP.Credentials = New NetworkCredential("xxx", "yyy")
  4.         oFTP.Method = WebRequestMethods.Ftp.DownloadFile
  5.         oFTP.KeepAlive = False
  6.         'oFTP.EnableSsl = UseSSL
  7.         'If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
  8.         oFTP.UseBinary = True
  9.         Dim response As FtpWebResponse = CType(oFTP.GetResponse, FtpWebResponse)
  10.         Dim responseStream As Stream = response.GetResponseStream
  11.         Dim fs As New FileStream(DestFile & "\" & Name, FileMode.Create)
  12.         Dim buffer(2047) As Byte
  13.         Dim read As Integer = 1
  14.         While read <> 0
  15.             read = responseStream.Read(buffer, 0, buffer.Length)
  16.             fs.Write(buffer, 0, read)
  17.         End While
  18.         responseStream.Close()
  19.         fs.Flush()
  20.         fs.Close()
  21.         responseStream.Close()
  22.         response.Close()
  23.         oFTP = Nothing
  24.         Return True
  25.     End Function
  26.  
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?
Mar 15 '11 #1
1 4801
no comment related to this topic, nothing?
Mar 24 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul | last post by:
Is it possible to delete a file on a client side machine using VB/JAVA script? My website allows the user to upload a file to the website and after that I would like to delete the file on their...
2
by: Sleepy | last post by:
I am wanting to allow a client to download a file from a web server and save it on their local PC. The only way that I have found to do this is to use HTTP headers. I am using the code shown below...
4
by: Nathan Sokalski | last post by:
I want to give visitors to my site the option of downloading a generated ..txt file by clicking a button. I know how to generate text files, but how do I cause the browser to pop up one of those...
2
by: TOI DAY | last post by:
Hi all, How can I delete the file on the server after the user download it? For example: I have file name "123.txt" on a server, I copy it to "ABC.txt", then allow uer download the...
2
by: Mark | last post by:
Here is what I am hoping to accomplish: 1. Connect to FTP server - I can do this. 2. Download file contained in the directory that I connect to. 3. Delete file contained on the ftp server. 4. ...
3
by: Parag Gaikwad | last post by:
Hi, I need to delete files across the network using web client (IE 6.x) in Win 2003 - IIS 6.0 environment. Can someone please suggest an approach I can use to acheive this. Will using FSO do...
5
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a process) and a text file that I created in Windows...
4
by: Ashok | last post by:
Dear Friends, How to download a file from server to client local pc without user intervention ie without the save as popup window. Can you give me step by step. Thanks
4
by: majik92 | last post by:
I want a user to download to download bec.exe from download.php. But, the next time anyone accesses download.php, i want it to turn up a Error 404 page or simply a sentence saying "sorry, this file...
1
by: gary2008 | last post by:
Greeting all, I was trying to use C# to download a file that needs log-in: string strDownloadURL = "http://www.cchere.com/list.rdl"; WebClient Client = new WebClient();...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.