Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ive been working on a program that syncs its data via SQL and FTP to our company server. I wrote the following subs and placed them in a module for easy FTP coding and they work fine but are incredibly slow for only uploading a couple Mb of data and i think the problem lays in there not large files but rather a large amount of files and thus making more FTP connections than necessary. I tried to cut the fat off this as much as possible but it always seems to slow on the deeper recursive directories....

Imports System.IO 

Public Module Sync

    Public Sub UploadFile(ByVal Game As String, ByVal Path As String, ByVal FileName As String, ByVal Source As String, ByVal User As String)

        Path = Path.Trim("/")

        If Not Path = "" Then

            MakeDir(Game, Path, User)

        End If

        Dim sr = New StreamReader(Source, System.Text.Encoding.Default)

        Dim RawFile As String = sr.ReadToEnd

        sr.Close()

        For i = 1 To 5

            Try

                Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://server.net/" + User + "/Profiles/" + Game + "/GameData/" + Path + "/" + FileName), System.Net.FtpWebRequest)

                clsRequest.Credentials = New System.Net.NetworkCredential("user", "pass")

                clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

                Dim bFile = System.Text.Encoding.Default.GetBytes(RawFile)

                Dim clsStream = _

                    clsRequest.GetRequestStream()

                clsStream.Write(bFile, 0, bFile.Length)

                clsStream.Close()

                clsStream.Dispose()

                Exit For

            Catch err As Exception

                If i = 5 Then

                    MsgBox("Unable to upload """ & Source & """ to """ & "ftp://dsdcomputers.net/" + User + "/Profiles/" + Game + "/GameData/" + Path + "/" + FileName & """ after 5 attempts because of the following error:" & vbCrLf & vbCrLf & err.ToString)

                End If

            End Try

        Next

    End Sub

    Public Sub MakeDir(ByVal Game As String, ByVal Path As String, ByVal User As String)

        Path = Path.Trim("/")

        If Path = Nothing Then

            Exit Sub

        End If

        If Path.Contains("/") Then

            Dim PathArray As String() = Path.Split("/")

            Path = ""

            For Each folder In PathArray

                Path = Path & "/" & folder

                Try

                    Path = Path.Trim("/")

                    Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://server.net/" + User + "/Profiles/" + Game + "/GameData/" + Path), System.Net.FtpWebRequest)

                    clsRequest.Credentials = New System.Net.NetworkCredential("user", "pass")

                    clsRequest.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory

                    Dim response = clsRequest.GetResponse()

                Catch err As Exception

                End Try

            Next

        Else

            Try

                Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://server.net/" + User + "/Profiles/" + Game + "/GameData/" + Path), System.Net.FtpWebRequest)

                clsRequest.Credentials = New System.Net.NetworkCredential("user", "pass")

                clsRequest.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory

                Dim response = clsRequest.GetResponse()

            Catch err As Exception

            End Try

        End If

    End Sub

    Public Sub UploadDir(ByVal Game As String, ByVal Path As String, ByVal SourceDir As String, ByVal User As String)

        MakeDir(Game, Path, User)

        SourceDir = SourceDir.Trim()

        For Each File In System.IO.Directory.GetFiles(SourceDir)

            UploadFile(Game, Path, File.Substring(File.LastIndexOf("\") + 1, File.Length - (File.LastIndexOf("\") + 1)), File, User)

        Next

        For Each Directory In System.IO.Directory.GetDirectories(SourceDir)

            UploadDir(Game, Path & "/" & Directory.Substring(Directory.LastIndexOf("\") + 1, Directory.Length - (Directory.LastIndexOf("\") + 1)), Directory, User)

        Next

    End Sub

End Module
Posted
Updated 2-Feb-11 15:12pm
v2

1 solution

part of the answer was with me forcing the FTP to try and make all directories of the path even if they already existed, i addad a simple check function as follows

    Public Function DoesDirExist(ByVal Game As String, ByVal Path As String, ByVal User As String)

        Try

            Dim WebClient As New WebClient()

            Dim DirStatus As String = WebClient.DownloadString("http://server.net/GameSync/" + User + "/Profiles/" + Game + "/GameData/" + Path)

            Return True

        Catch err As Exception

            Return False

        End Try

    End Function 

and also put a quick if/then into the first lines of  makedir() right after the null check

        If DoesDirExist(Game, Path, User) = True Then

            Exit Sub

        End If 

so ths helps avoid some pointless loops, but not the increase in speed that i really need here... 

 
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