Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I’m working on part of my project which uploads new files to the FTP server what I’m trying to do is to check to see if the file is already on the FTP server and if it is then move on to the next one if not then it needs to be uploaded.
All the current items on the FTP server get listed into a list view control the first item shows the current directory which can be ignored this works perfectly.
The problem I have is I can’t keep the for loops synchronised with each other someone may have a better way of approaching this?
Below is my code:
VB.NET
Try

            ListRemoteDirectory(RemoteDirc) 

Dim itm As New ListViewItem

            For Each item In lvwRemote.Items
                itm = item
With itm
                    For Each fileNameA As String In Directory.GetFiles(CueDir, "*.m3u", SearchOption.AllDirectories)

                        If fileNameA.Contains("(") Then
                            GoTo SyncUp
                        Else
                            If itm.Text = ".." Then
                                GoTo SyncUp
                                'do nothing
                            Else


                                Dim JustTheFileName As String = IO.Path.GetFileName(fileNameA)
                                If JustTheFileName = itm.Text Then
                                    MsgBox("Match " & itm.Text)
                                    
                                Else
                                    MsgBox("No Match, Upload")
                                    Dim fi As New FileInfo(fileNameA)
                                    client.Upload(fi)
                                    txtLog.AppendText(JustTheFileName & " uploaded")
                                   
                                End If
                            End If
                        End If

                    Next

SyncUp:
                End With
                MsgBox(itm.Text)
            Next




        Catch ex As Exception
            MsgBox(ex.Message & "Cannot Upload")
        End Try
Posted
Comments
Basmeh Awad 11-Jan-16 8:34am    
are you using REBEX to upload files?
Pete_123 11-Jan-16 8:41am    
No im using https://code.google.com/p/ftpexplorer/

1 solution

Okay so after a fair few hours I stumbled across Pete Mourfield article on code project and I was able to get the result required therefore I’m going to post this solution in case anyone else has the same problem.

VB.NET
Public Function CheckIfFtpFileExists(ByVal fileUri As String) As Boolean
       Dim request As FtpWebRequest = WebRequest.Create(fileUri)
       request.Credentials = New NetworkCredential("Username", "Password")
       request.Method = WebRequestMethods.Ftp.GetFileSize
       Try
           Dim response As FtpWebResponse = request.GetResponse()
           ' THE FILE EXISTS
       Catch ex As WebException
           Dim response As FtpWebResponse = ex.Response
           If FtpStatusCode.ActionNotTakenFileUnavailable = response.StatusCode Then
               ' THE FILE DOES NOT EXIST
               Return False
           End If
       End Try
       Return True
   End Function


To call the function

C#
 Dim CueDir As String = "C:\Users\DaBeast\Documents\Numark CUE\Playlists"
        For Each fileNameA As String In Directory.GetFiles(CueDir, "*.m3u", SearchOption.AllDirectories)
            Dim JustTheFileName As String = IO.Path.GetFileName(fileNameA)
            If fileNameA.Contains("(") Then
                GoTo GoToNextFile
            Else

                If CheckIfFtpFileExists("ftp://severaddress.com:333/PR/Playlists/" & JustTheFileName) Then
                    MsgBox(JustTheFileName & " This file is on the server")
                Else
                    MsgBox(JustTheFileName & "This file is not on the server")
                    Dim fi As New FileInfo(fileNameA)
                    client.Upload(fi)
                    txtLog.AppendText(JustTheFileName & " uploaded")
                End If
            End If

GoToNextFile:
        Next
 
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