Advertisement
Mikestriken

GPT Paste

Nov 20th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function GetNextFile(FolderPath As String, FilePattern As String) As String
  2.     Static FilePaths() As String
  3.     Static CurrentIndex As Long
  4.     Static Initialized As Boolean
  5.    
  6.     Dim FileName As String
  7.     Dim SubFolder As String
  8.     Dim FSO As Object
  9.     Dim Folder As Object
  10.     Dim SubFolders As Object
  11.     Dim TempList As Collection
  12.     Dim i As Long
  13.  
  14.     ' Initialize on the first call
  15.    If Not Initialized Then
  16.         ' Create a temporary list to store file paths
  17.        Set TempList = New Collection
  18.         Set FSO = CreateObject("Scripting.FileSystemObject")
  19.         Set Folder = FSO.GetFolder(FolderPath)
  20.         Set SubFolders = Folder.SubFolders
  21.  
  22.         ' Add files from the current folder
  23.        FileName = Dir(FolderPath & "\" & FilePattern)
  24.         Do While FileName <> ""
  25.             TempList.Add FolderPath & "\" & FileName
  26.             FileName = Dir
  27.         Loop
  28.  
  29.         ' Add files from subfolders recursively
  30.        For Each SubFolder In SubFolders
  31.             AddFilesFromSubfolder SubFolder.Path, FilePattern, TempList
  32.         Next SubFolder
  33.  
  34.         ' Transfer file paths from the collection to a static array
  35.        ReDim FilePaths(1 To TempList.Count)
  36.         For i = 1 To TempList.Count
  37.             FilePaths(i) = TempList(i)
  38.         Next i
  39.  
  40.         ' Initialize index and flag
  41.        CurrentIndex = 1
  42.         Initialized = True
  43.     End If
  44.  
  45.     ' Return the next file path or an empty string if no more files
  46.    If CurrentIndex <= UBound(FilePaths) Then
  47.         GetNextFile = FilePaths(CurrentIndex)
  48.         CurrentIndex = CurrentIndex + 1
  49.     Else
  50.         GetNextFile = "" ' No more files
  51.    End If
  52. End Function
  53.  
  54. Private Sub AddFilesFromSubfolder(FolderPath As String, FilePattern As String, TempList As Collection)
  55.     Dim FileName As String
  56.     Dim SubFolder As String
  57.     Dim FSO As Object
  58.     Dim SubFolders As Object
  59.  
  60.     ' Initialize FileSystemObject and get subfolders
  61.    Set FSO = CreateObject("Scripting.FileSystemObject")
  62.     Set SubFolders = FSO.GetFolder(FolderPath).SubFolders
  63.  
  64.     ' Add files from the current folder
  65.    FileName = Dir(FolderPath & "\" & FilePattern)
  66.     Do While FileName <> ""
  67.         TempList.Add FolderPath & "\" & FileName
  68.         FileName = Dir
  69.     Loop
  70.  
  71.     ' Recursively add files from subfolders
  72.    For Each SubFolder In SubFolders
  73.         AddFilesFromSubfolder SubFolder.Path, FilePattern, TempList
  74.     Next SubFolder
  75. End Sub
  76.  
  77. Sub TestGetNextFile()
  78.     Dim FolderPath As String
  79.     Dim FilePattern As String
  80.     Dim FilePath As String
  81.  
  82.     FolderPath = "C:\Your\Folder\Path" ' Replace with your folder path
  83.    FilePattern = "*.ext" ' Replace with your file extension
  84.  
  85.     ' Retrieve files one at a time
  86.    Do
  87.         FilePath = GetNextFile(FolderPath, FilePattern)
  88.         If FilePath = "" Then Exit Do
  89.         Debug.Print FilePath ' Replace with your desired processing
  90.    Loop
  91. End Sub
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement