USO DE LA CLASE FTP

'-----
 Dim docs As New cFTP(FTP, portFTP)
 'Verificar credenciales
 docs.IdVerify(userFTP, passFTP)
 If docs.Connected Then
     If docs.DirList.Length > 0 Then
         listarFTP(FTP, DFTP, userFTP, passFTP)  'RUTINA ABAJO DETALLADA (Devuelve el contenido de la FTP en el ListView1)
         For i = 0 To ListView1.Items.Count - 1
         'Recogemos los nombres de todos los ficheros en el array ficheros
              ficheros(i) = ListView1.Items(j).Text 
         Next
      end if
 end if
'-----


'RUTINA QUE LISTA EL CONTENIDO DE SITIO FTP I LO EXPONE EN UN LISTVIEW
Sub listarFTP(ByRef adressFTP As String, ByVal directoryFTP As String, ByVal userFTP As String, ByVal passwFTP As String)

        Dim strList(250) As String
        Dim cSep() As Char = {vbCr, vbLf}
        Dim c As String, i As Integer
        Dim strItems() As String
        Dim oItem As ListViewItem
        '----
        If directoryFTP <> "" Then directoryFTP = "/" & directoryFTP
        Dim ftpWebReq As Net.FtpWebRequest = CType(Net.WebRequest.Create("ftp://" & adressFTP & directoryFTP), Net.FtpWebRequest)
        ftpWebReq.Credentials = New Net.NetworkCredential(userFTP, passwFTP)
        ftpWebReq.Method = Net.WebRequestMethods.Ftp.ListDirectoryDetails
        ftpWebReq.Proxy = Nothing
        Dim ftpWebResp As Net.FtpWebResponse = CType(ftpWebReq.GetResponse(), Net.FtpWebResponse)
        Dim streamer As IO.Stream = ftpWebResp.GetResponseStream()
        Dim reader As New IO.StreamReader(streamer)
        'Dim s As String = reader.ReadToEnd()
        Dim n As Integer = 0, linea As String


        While Not (reader.EndOfStream)
            linea = reader.ReadLine
            If InStr(linea, ".xml", CompareMethod.Text) Then 'En este caso solo busco ficheros XML
                strList(n) = linea
                n = n + 1
            End If
        End While


        Dim formato2 As Boolean
        For i = 0 To n - 1
            c = strList(i)
            If InStr(c, "group") <> 0 Then
                c = Trim(Mid(c, InStr(c, "group") + 5))
                formato2 = True
            End If


            If c.Length > 0 Then
                strItems = c.Split(" ")
                oItem = New ListViewItem()
                oItem.SubItems.Add("")
                oItem.SubItems.Add("")
                oItem.SubItems.Add("")
                oItem.SubItems.Add("")
                                        If InStr(c, ".xml") <> 0 Then  'En este caso solo busco ficheros XML
                    If formato2 Then '2 formatos posibles de como el sitio FTP no puede devolver la información de fichero
                        oItem.SubItems(1).Text = Trim(Mid(c, 1, InStr(c, " ") - 1)) 'Tamaño
                        c = Trim(Replace(c, oItem.SubItems(1).Text, ""))
                        oItem.SubItems(2).Text = Mid(c, 1, 6) 'Fecha
                        c = Trim(Replace(c, oItem.SubItems(2).Text, ""))
                        oItem.SubItems(3).Text = Mid(c, 1, 5) 'Hora
                        c = Trim(Replace(c, oItem.SubItems(3).Text, ""))
                        oItem.SubItems(0).Text = Trim(c) 'Fichero
                    Else 
                        oItem.SubItems(2).Text = Mid(c, 1, 8) 'Fecha
                        c = Trim(Replace(c, oItem.SubItems(2).Text, ""))
                        oItem.SubItems(3).Text = Mid(c, 1, 7) 'Hora
                        c = Trim(Replace(c, oItem.SubItems(3).Text, ""))
                        oItem.SubItems(1).Text = Trim(Mid(c, 1, InStr(c, " ") - 1)) 'Tamaño
                        c = Trim(Replace(c, oItem.SubItems(1).Text, ""))
                        oItem.SubItems(0).Text = extraerNOMFICHERO(c) 'Fichero
                    End If
                    
                    If oItem.SubItems(1).Text.Length = 0 Then oItem.SubItems(1).Text = "





"
                    If oItem.SubItems(1).Text = "
" Then
                        oItem.ImageIndex = 0
                    Else
                        oItem.ImageIndex = 1
                    End If
                    ListView1.Items.Add(oItem)
                    oItem = Nothing
                End If
            End If
        Next
End Sub
Function extraerNOMFICHERO(ByVal c As String) As String
'Recursiva que nos devuelve el nombre del fichero'En el formato 2 está al final de la cadena   If InStr(Trim(c), " ") > 0 Then
      c = Trim(Mid(c, InStr(c, " ") + 1))
      c = extraerNOMFICHERO(c)
   End If
   Return c
End Function

No hay comentarios:

Publicar un comentario