检查FTP服务器上的文件,如果存在不上传它们? [英] Checking files on the FTP server if the exist dont upload them?

查看:93
本文介绍了检查FTP服务器上的文件,如果存在不上传它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我正在处理我的项目的一部分,它将新文件上传到FTP服务器我正在尝试做的是检查文件是否已经在FTP服务器上,如果是,那么继续如果不是那么下一个则需要上传。

FTP服务器上的所有当前项目都列在列表视图控件中,第一项显示当前目录,可以忽略这个目录。

我遇到的问题是我无法保持for循环彼此同步,有人可能有更好的方法来接近这个吗?

以下是我的代码:

 尝试 

ListRemoteDirectory(RemoteDirc)

Dim itm As ListViewItem

对于 每个 item lvwRemote.Items
itm = item
使用 itm
对于 每个 fileNameA 作为 字符串 Directory.GetFiles(CueDir, * .m3u,SearchOption.AllDirectories)

If fileNameA.Contains( 然后
GoTo SyncUp
否则
如果 itm .Text = .. 然后
GoTo SyncUp
' do没有
其他


Dim JustTheFileName As String = IO.Path.GetFileName(fileNameA)
如果 JustTheFi leName = itm.Text 然后
MsgBox( 匹配& itm.Text)

其他
MsgBox( No Match,Upload
Dim fi As FileInfo(fileNameA)
client.Upload(fi)
txtLog.AppendText(JustTheFileName& 已上传

结束 如果
结束 如果
结束 如果

下一步

SyncUp:
结束 使用
MsgBox(itm.Text)
下一步




Catch ex 作为例外
MsgBox(ex.Message& 无法上传
结束 尝试

解决方案

好的,经过几个小时后,我偶然发现在Pete Mourfield关于代码项目的文章中,我能够得到所需的结果,因此我将发布此解决方案,以防其他人遇到同样的问题。



 公共 功能 CheckIfFtpFileExists( ByVal  fileUri  As   String 作为 布尔 
Dim request As FtpWebRequest = WebRequest.Create(fileUri)
request.Credentials = New NetworkCredential( 用户名 密码
request.Method = WebRequestMethods.Ftp.GetFileSize
尝试
Dim 响应作为 FtpWebResponse = request.GetResponse()
' 文件存在
Catch ex As WebException
Dim response As FtpWebResponse = ex.Response
如果 FtpStatusCode.ActionNotTakenFileUnavailable = response.StatusCode 那么
' 文件不存在
返回 错误
结束 如果
结束 尝试
返回 True
结束 功能





调用函数



 Dim CueDir As  String  =   C:\ Users \DaBeast \Documents\Numark CUE \Playlists 
For Each fileNameA As < span class =code-sdkkeyword> String 在Directory.GetFiles中(CueDir, *。m3u ,SearchOption.AllDirectories)
Dim JustTheFileName As String = IO.Path.GetFileName(fileNameA)
if fileNameA.Contains(< span class =code-string> )然后
GoTo GoToNextFile
否则

如果CheckIfFtpFileExists( ftp://severaddress.com:333/PR/Pl aylists /& JustTheFileName)然后
MsgBox(JustTheFileName& 此文件在服务器上
否则
MsgBox(JustTheFileName& 此文件不在服务器上
Dim fi As New FileInfo(fileNameA)
client.Upload(fi)
txtLog.AppendText(JustTheFileName& < span class =code-string> upload)
End if
End if

GoToNextFile:
Next


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:

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

解决方案

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.

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

 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


这篇关于检查FTP服务器上的文件,如果存在不上传它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆