ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] vba Ū¨ú ftp ¸Ì­±ªºÀɮײM³æ

¦^´_  Joforn


³oVBA°õ¦æOK¡A¥i¬O¤¤¤åÀɮצWºÙ¦^¶ÇÅܦ¨¶Ã½X¡A½Ð°Ý³o­n¦p¦ó­×¥¿¡A·P®¦

²Ä¤@­ÓVBA°õ¦æ ...
ML089 µoªí©ó 2020-11-11 14:17


.Pathªð¦^ªº¦}¤£¬O¶Ã½X¡A¦Ó¬O±N¤å¦r¶i¦æ¤FUrl½s½X¡A¥i¥H³q¹LUrl¸Ñ½X±o¨ì­ì¦r²Å¦ê¡A¤]¥i¥H¥Î¤U­±ªº¤è¦¡ª½±µªð¦^¤å¦r®æ¦¡¡C
  1. Public Property Get FTPSearch(ByVal FTPUrl As String, _
  2.         Optional ByVal UserName As String, _
  3.         Optional ByVal PassWord As String, _
  4.         Optional ByVal Port As Integer) As Collection
  5.                            
  6.   Dim objShell  As Object, objFolder As Object
  7.   
  8.   On Error Resume Next
  9.   
  10.   If Len(FTPUrl) Then
  11.     If Len(UserName) Then FTPUrl = UserName & ":" & PassWord & "@" & FTPUrl
  12.     If Port > 0 Then FTPUrl = FTPUrl & ":" & Port
  13.     Set objShell = CreateObject("Shell.Application")
  14.     Set objFolder = objShell.Namespace("FTP://" & FTPUrl)
  15.   End If
  16.   Set FTPSearch = New Collection
  17.   If objFolder Is Nothing Then Exit Function
  18.   
  19.   With objFolder
  20.     If .ParentFolder.Self.IsFolder Then
  21.       If .ParentFolder.ParseName(.Self.Name) Is Nothing Then
  22.         Debug.Print "Error:Invalid Path!"
  23.       ElseIf objFolder.Self.IsBrowsable Then
  24.         Debug.Print "Error:Invalid Parameter!"
  25.       ElseIf objFolder.Self.IsFolder Then
  26.         Debug.Print "Start search files in path..."
  27.         With objFolder
  28.           FTPSearchWithShell FTPSearch, .ParentFolder.ParseName(.Self.Name).GetFolder
  29.         End With
  30.         Debug.Print "Files search completed."
  31.       End If
  32.     ElseIf objFolder.Self.IsFolder Then
  33.       Debug.Print "Start search files in path..."
  34.       With objFolder
  35.         FTPSearchWithShell FTPSearch, objFolder
  36.       End With
  37.       Debug.Print "Files search completed."
  38.     End If
  39.   End With
  40. End Property

  41. Private Sub FTPSearchWithShell(ByVal Searched As Collection, ByVal Folder As Object, Optional ByVal strPath As String)
  42.   Dim FolderItem  As Object
  43.   Dim objFolder   As Object
  44.   Dim subSearch   As New Collection
  45.   
  46.   On Error Resume Next
  47.   
  48.   If Len(strPath) = 0 Then
  49.     Set objFolder = Folder
  50.     Do While (objFolder.ParentFolder.Self.IsFolder)
  51.       strPath = objFolder.Self.Name & "/" & strPath
  52.       Set objFolder = objFolder.ParentFolder
  53.     Loop
  54.     strPath = objFolder.Self.Path & strPath
  55.   End If
  56.   
  57.   If Folder.Items.Count > 0 Then
  58.     Set subSearch = New Collection
  59.     For Each FolderItem In Folder.Items
  60.       With FolderItem
  61.         If .IsFolder Then
  62.           Set Folder = .GetFolder
  63.           If Folder.Items.Count Then subSearch.Add Folder
  64.         ElseIf .IsBrowsable Then
  65.           Searched.Add strPath & .Name
  66.         End If
  67.       End With
  68.     Next FolderItem
  69.    
  70.     For Each Folder In subSearch
  71.       FTPSearchWithShell Searched, Folder
  72.       DoEvents
  73.     Next Folder
  74.   End If
  75. End Sub

  76. Sub Test()
  77.   Dim Files As Collection
  78.   Dim I     As Long
  79.   
  80.   Set Files = FTPSearch("118.163.50.55/firmware_software_version_files/", "xxxxx", "xxxx")
  81.   For I = 1 To Files.Count
  82.     Debug.Print Files.Item(I)
  83.   Next I
  84. End Sub
½Æ»s¥N½X
¥@¬É¨º»ò¤j¡A¥i§Ú·Q¥h­þ¡H

TOP

¦^´_ 11# Joforn

·PÁ¡A¤¤¤åÀɦW¥i¥H¥¿±`Åã¥Ü
{...} ªí¥Ü»Ý­n¥Î CTRL+SHIFT+ENTER ¤TÁä¿é¤J¤½¦¡

TOP

        ÀR«ä¦Û¦b : ¹D¼w¬O´£ª@¦Û§Úªº©ú¿O¡A¤£¸Ó¬O¨þ¥¸§O¤HªºÃ@¤l¡C
ªð¦^¦Cªí ¤W¤@¥DÃD