Results 1 to 17 of 17

Thread: [RESOLVED] FTP Download Whole Dir

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Resolved [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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    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")

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    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...)

  6. #6
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    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...

  8. #8
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: FTP Download Whole Dir

    Sorry for that..
    Have a look at the last post: http://social.msdn.microsoft.com/for...-08e71e98fa83/


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: FTP Download Whole Dir

    spending $299 for library? nah, id' rather do it with ftpwebrequest :P

  10. #10
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: FTP Download Whole Dir

    Read! 'cause that isn't the last post.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    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

  12. #12
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    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.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: FTP Download Whole Dir

    doesnt work, i tried it earlier
    also, there isn't any example that downloads whole directory...

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    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...

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    Re: FTP Download Whole Dir

    still need help with this

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    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

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Location
    Croatia
    Posts
    201

    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width