2016-07-15 2 views

Antwort

2

Ich erstelle Klasse namens FileInformation und eine Funktion getFileList, die ein Array von FileInformation zurückgibt. GetFTPFileList können Sie getFileList mit einem FTP-Server verwenden.

enter image description here

Klasse Fileinformation

 

    Option Explicit 

    Public FilePath 
    Public FolderPath 
    Public FileExtension 

    Public Sub setValues(localRoot, fso, f) 
     FilePath = f.Path 
     FolderPath = f.ParentFolder.Path 
     FileExtension = fso.GetExtensionName(FilePath) 
    End Sub 

Der Rest des Codes geht in einen normalen Code-Modul.

 
    Sub PrintFileInformation() 
     Const Username As String = "" 
     Const Password As String = "" 
     Const ftpSite As String = "" 
     Const ftpRoot As String = "" 
     Dim arFiles 
     Dim i As Long 

     arFiles = getFTPFileList(Username, Password, ftpSite, ftpRoot) 

     For i = 0 To UBound(arFiles) 
      Debug.Print arFiles(i).FilePath 
      Debug.Print arFiles(i).FolderPath 
      Debug.Print arFiles(i).FileExtension 
     Next 

    End Sub 

    Function getFTPFileList(Username As String, Password As String, ftpSite As String, ftpRoot As String) 

     ftpRoot = "ftp://" & Username & ":" & Password & "@" & ftpSite & "/" & ftpRoot 
     getFTPFileList = getFileList(ftpRoot) 

    End Function 

    Function getFileList(localRoot As String, Optional fld, Optional ftpArray) 
     Dim fso, f, baseFolder, subFolder, ftpFile, i 

     Set fso = CreateObject("Scripting.Filesystemobject") 

     If IsMissing(fld) Then 
      Set baseFolder = fso.GetFolder(localRoot) 
     Else 
      Set baseFolder = fld 
     End If 

     For Each f In baseFolder.Files 

      If IsMissing(ftpArray) Then 
       ReDim ftpArray(0) 
      Else 
       i = UBound(ftpArray) + 1 
       ReDim Preserve ftpArray(i) 
      End If 
      Set ftpFile = New FileInformation 
      ftpFile.setValues localRoot, fso, f 
      Set ftpArray(i) = ftpFile 

     Next 

     For Each subFolder In baseFolder.SubFolders 
      getFileList localRoot, subFolder, ftpArray 
     Next 

     getFileList = ftpArray 
    End Function 

Verwandte Themen