[RESOLVED] FTP Download Whole Dir
Hi!
As title says, i need to download whole directory, including all files in it from FTP, and its pretty difficult to find working FTP download example (googling and searching for 2 hours now, and all i found was upload example)
Thanks for help in advance!
Re: FTP Download Whole Dir
This is one of those "how do I crack a carton full of eggs" questions. If you know how to crack one egg then you know how to crack a carton full. Same goes here: if you know how to download one file then you know how to download a folder full. If you want to download subfolders too then you'll create a recursive method. After downloading each file in the folder you would then call the same method for each subfolder.
So, do you know how to download one file?
Re: FTP Download Whole Dir
this is what seems to work, tho i cant find the downloaded file anywhere (where is it supposed to be, and can i set the target to save?)
Code:
Dim file As String = (filepath)
Dim request As FtpWebRequest = CType(WebRequest.Create(file), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
request.Credentials = New NetworkCredential("username", "pass")
Re: FTP Download Whole Dir
That code doesn't actually download anything. It simply creates the request. If you want the data you have to get a response, which you do by calling GetResponse. That will return an FtpWebResponse, so you should check out the documentation for that, which I'm sure includes a code example.
Re: FTP Download Whole Dir
Really, i checked at msdn, and as in most examples it says that vb.net code isnt supported...
and i really cant find a working example anywhere (except what i have, and that isnt working too...)
Re: FTP Download Whole Dir
Re: FTP Download Whole Dir
saw that, but that doesnt help me much, as i dont have a static ftp (depends on user)
and on other example it uses chilkat library, which i really wouldnt like to use...
Re: FTP Download Whole Dir
Re: FTP Download Whole Dir
spending $299 for library? nah, id' rather do it with ftpwebrequest :P
Re: FTP Download Whole Dir
Read! 'cause that isn't the last post.
Re: FTP Download Whole Dir
Thanks for that thread.
Now i actually can download single file:
Code:
Dim pth As String = Form1.Label9.Text.Substring(0, Form1.Label9.Text.Length - 3)
Const localFile As String = "C:\PBSS\ident.png"
Dim username As String = Form1.TextBox1.Text
Dim password As String = Form1.TextBox2.Text
Dim URI As String = ("ftp://" & pth & "/" & pth & " port 28960/pb/svss/ident.png")
Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
ftp.Credentials = New System.Net.NetworkCredential(username, password)
ftp.KeepAlive = False
ftp.UseBinary = True
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
'loop to read & write to file
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using
problem is that i always have to set it's name, adn i cant download all files in directory (whole directory)
so if you have any info about that, it would be awesome if you shared it with me
Re: FTP Download Whole Dir
Something like:
Dim URI As String = ("ftp://" & pth & "/" & pth & " port 28960/pb/svss/*")
Not sure if it will work, but you could try it. There are more examples in the last post I mentioned. ;)
Re: FTP Download Whole Dir
doesnt work, i tried it earlier
also, there isn't any example that downloads whole directory...
Re: FTP Download Whole Dir
hmm, how would it be if i checked for filenames, and if some exist, then download it?
is there any solution ot this?? i cant figure anything out...
Re: FTP Download Whole Dir
still need help with this
Re: FTP Download Whole Dir
figured some things out, tho it still isnt downloading whole directory, but only filesthat i actually need:
Code:
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim pth As String = Form1.Label9.Text.Substring(0, Form1.Label9.Text.Length - 3)
Dim ftpreq As FtpWebRequest = FtpWebRequest.Create("ftp://" & pth & "/" & pth & " port 28960/pb/svss/")
With ftpreq
.Credentials = New NetworkCredential(Form1.TextBox1.Text, Form1.TextBox2.Text)
.Method = WebRequestMethods.Ftp.ListDirectory
End With
Dim sr As New StreamReader(ftpreq.GetResponse().GetResponseStream())
Dim str As String = sr.ReadLine()
While Not str Is Nothing
ListBox1.Items.Add(str)
str = sr.ReadLine()
End While
sr.Close()
sr = Nothing
ftpreq = Nothing
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim pth As String = Form1.Label9.Text.Substring(0, Form1.Label9.Text.Length - 3)
Dim localFile As String = "C:\PBSS\" & ListBox1.SelectedItem
Dim username As String = Form1.TextBox1.Text
Dim password As String = Form1.TextBox2.Text
Dim URI As String = ("ftp://" & pth & "/" & pth & " port 28960/pb/svss/" & ListBox1.SelectedItem)
Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
ftp.Credentials = New System.Net.NetworkCredential(username, password)
ftp.KeepAlive = False
ftp.UseBinary = True
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
'loop to read & write to file
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using
End Sub
hope this helps someone :D
Re: FTP Download Whole Dir
I didnt want to make a new thread, so il post the final solution here
This code will get the directory list from ftp, download all pictures that are in it on local disk
Code:
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim pth As String = Form1.Label9.Text.Substring(0, Form1.Label9.Text.Length - 3)
Dim ftpreq As FtpWebRequest = FtpWebRequest.Create("ftp://directory_path")
With ftpreq
.Credentials = New NetworkCredential(Form1.TextBox1.Text, Form1.TextBox2.Text)
.Method = WebRequestMethods.Ftp.ListDirectory
End With
Dim sr As New StreamReader(ftpreq.GetResponse().GetResponseStream())
Dim str As String = sr.ReadLine()
While Not str Is Nothing
ListBox1.Items.Add(str)
str = sr.ReadLine()
End While
sr.Close()
sr = Nothing
ftpreq = Nothing
Dim files = (From file In ListBox1.Items _
Let ext = IO.Path.GetExtension(file.ToString) _
Where ext = ".jpg" OrElse ext = ".gif" OrElse ext = ".bmp" OrElse ext = ".png" _
Select file).ToArray
ListBox1.Items.Clear()
ListBox1.Items.AddRange(files)
download()
End Sub
Function download()
If Not Directory.Exists("C:\LocalDir") Then
Directory.CreateDirectory("C:\LocalDir")
End If
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim pth As String = Form1.Label9.Text.Substring(0, Form1.Label9.Text.Length - 3)
Dim localFile As String = "C:\LocalDir\" & ListBox1.Items(i)
Dim username As String = Form1.TextBox1.Text
Dim password As String = Form1.TextBox2.Text
Dim URI As String = ("ftp://directory_path" & ListBox1.Items(i))
Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
ftp.Credentials = New System.Net.NetworkCredential(username, password)
ftp.KeepAlive = False
ftp.UseBinary = True
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
'loop to read & write to file
Using fs As New IO.FileStream(localFile, IO.FileMode.OpenOrCreate)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using
Next
Return 0
End Function
was pretty easy once i figured out how to loop through listbox
hope this helps someone (as i didn't see any example that works so far)