|
| 1 | +VERSION 1.0 CLASS |
| 2 | +BEGIN |
| 3 | + MultiUse = -1 'True |
| 4 | +END |
| 5 | +Attribute VB_Name = "DirectoryManager" |
| 6 | +Attribute VB_GlobalNameSpace = False |
| 7 | +Attribute VB_Creatable = False |
| 8 | +Attribute VB_PredeclaredId = False |
| 9 | +Attribute VB_Exposed = False |
| 10 | +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 11 | +'Version 1.0.0 ' |
| 12 | +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 13 | +'MIT License ' |
| 14 | +' ' |
| 15 | +'Copyright (c) 2022 M. Scott Lassiter ' |
| 16 | +' ' |
| 17 | +'Permission is hereby granted, free of charge, to any person obtaining a copy ' |
| 18 | +'of this software and associated documentation files (the "Software"), to deal ' |
| 19 | +'in the Software without restriction, including without limitation the rights ' |
| 20 | +'to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ' |
| 21 | +'copies of the Software, and to permit persons to whom the Software is ' |
| 22 | +'furnished to do so, subject to the following conditions: ' |
| 23 | +' ' |
| 24 | +'The above copyright notice and this permission notice shall be included in all ' |
| 25 | +'copies or substantial portions of the Software. ' |
| 26 | +' ' |
| 27 | +'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ' |
| 28 | +'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ' |
| 29 | +'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ' |
| 30 | +'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ' |
| 31 | +'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ' |
| 32 | +'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ' |
| 33 | +'SOFTWARE. ' |
| 34 | +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 35 | + |
| 36 | +Option Explicit |
| 37 | + |
| 38 | +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 39 | +' Class Variables |
| 40 | +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 41 | + |
| 42 | +Dim FolderPath As String |
| 43 | +Dim FolderName As String |
| 44 | + |
| 45 | +Dim FoundFoldersList As New Collection |
| 46 | +Dim FoundFilesList As New Collection |
| 47 | + |
| 48 | +Dim FoundFolders As New Collection |
| 49 | +Dim FoundFiles As New Collection |
| 50 | + |
| 51 | +Dim isFile As Boolean |
| 52 | +Dim OmittedPrefixValue As String |
| 53 | + |
| 54 | +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 55 | +' Properties |
| 56 | +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 57 | + |
| 58 | +Public Property Get Path() As String |
| 59 | + Path = FolderPath |
| 60 | +End Property |
| 61 | +Public Property Let Path(PathName As String) |
| 62 | +'This is the entry point to initialize the class. Trying to use any feature before running this script should abort. |
| 63 | + FolderPath = PathName |
| 64 | + If Not (Exists) Then Exit Property |
| 65 | + FolderPath = FormatFilePath(FolderPath) |
| 66 | + |
| 67 | + 'Reinitialize if the same DirectoryManager class is set to a new path |
| 68 | + Set FoundFoldersList = New Collection |
| 69 | + Set FoundFilesList = New Collection |
| 70 | + Set FoundFolders = New Collection |
| 71 | + Set FoundFiles = New Collection |
| 72 | + |
| 73 | + FindFilesAndFolders |
| 74 | + FindSubFilesAndFolders |
| 75 | +End Property |
| 76 | + |
| 77 | + |
| 78 | +Public Property Get Name() As String |
| 79 | + If isFile Then |
| 80 | + Name = Split(FolderPath, "\")(UBound(Split(FolderPath, "\"))) |
| 81 | + Else |
| 82 | + Name = Split(FolderPath, "\")(UBound(Split(FolderPath, "\")) - 1) |
| 83 | + End If |
| 84 | +End Property |
| 85 | + |
| 86 | + |
| 87 | +Public Property Get Folders() As Collection |
| 88 | + Set Folders = FoundFolders |
| 89 | +End Property |
| 90 | + |
| 91 | + |
| 92 | +Public Property Get Files() As Collection |
| 93 | + Set Files = FoundFiles |
| 94 | +End Property |
| 95 | + |
| 96 | + |
| 97 | +Public Property Get Exists() As Boolean |
| 98 | +'Uninitialized instances of the class and folders that do not exist return false |
| 99 | + |
| 100 | + On Error Resume Next |
| 101 | + If Len(Dir(FolderPath, vbDirectory)) = 0 Or FolderPath = "" Or Err <> 0 Then 'Gives error 52 if file name is invalid |
| 102 | + Exists = False |
| 103 | + Else |
| 104 | + Exists = True |
| 105 | + End If |
| 106 | + On Error GoTo 0 |
| 107 | + |
| 108 | +End Property |
| 109 | + |
| 110 | + |
| 111 | +Public Property Let OmittedPrefix(Omit As String) |
| 112 | +'If true, the DirectoryManager ignores all files and folders that begin with the specified characters. |
| 113 | +' This allows the end user to setup a file structure with folders or files that he or she does not want |
| 114 | +' to be included when the DirectoryManager scans a path. |
| 115 | + OmittedPrefixValue = Omit |
| 116 | + Path = FolderPath 'Reinitialize the DirectoryManager, this time using the new omit prefix |
| 117 | +End Property |
| 118 | +Public Property Get OmittedPrefix() As String |
| 119 | + OmittedPrefix = OmittedPrefixValue |
| 120 | +End Property |
| 121 | + |
| 122 | + |
| 123 | +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 124 | +' Functions and Methods |
| 125 | +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 126 | + |
| 127 | +Private Sub FindFilesAndFolders() |
| 128 | +'Loops through all files and folders in this path directory and adds them to their respective collections |
| 129 | + |
| 130 | + Dim RefFolders As Variant |
| 131 | + Dim newItem As DirectoryManager |
| 132 | + |
| 133 | + RefFolders = Dir(FolderPath, vbDirectory) |
| 134 | + Do While RefFolders <> "" And isFile = False |
| 135 | + |
| 136 | + 'Ignore the special folders '.' and '..' |
| 137 | + If RefFolders <> "." And RefFolders <> ".." Then |
| 138 | + If Left(RefFolders, Len(OmittedPrefixValue)) <> OmittedPrefixValue Or OmittedPrefixValue = "" Then 'Ignore the omitted prefixes, if specified |
| 139 | + If (GetAttr(FolderPath & RefFolders) And vbDirectory) = vbDirectory Then |
| 140 | + FoundFoldersList.Add RefFolders, RefFolders |
| 141 | + Else |
| 142 | + FoundFilesList.Add RefFolders, RefFolders |
| 143 | + End If |
| 144 | + End If |
| 145 | + End If |
| 146 | + |
| 147 | + RefFolders = Dir 'Required to move to the next file |
| 148 | + Loop |
| 149 | + |
| 150 | +End Sub |
| 151 | + |
| 152 | + |
| 153 | +Private Sub FindSubFilesAndFolders() |
| 154 | +'After the list of folders is identified, this function recursively creates a new Folder class for each folder |
| 155 | +' and repeats the process. |
| 156 | + Dim item As Variant |
| 157 | + Dim newFolder As DirectoryManager |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + For Each item In FoundFoldersList |
| 162 | + Set newFolder = New DirectoryManager |
| 163 | + newFolder.OmittedPrefix = OmittedPrefixValue |
| 164 | + newFolder.Path = FolderPath & item |
| 165 | + |
| 166 | + InsertCollectionValueAlphabetically FoundFolders, newFolder, newFolder.Name |
| 167 | + Next item |
| 168 | + |
| 169 | + For Each item In FoundFilesList |
| 170 | + Set newFolder = New DirectoryManager |
| 171 | + newFolder.OmittedPrefix = OmittedPrefixValue |
| 172 | + newFolder.Path = FolderPath & item |
| 173 | + |
| 174 | + InsertCollectionValueAlphabetically FoundFiles, newFolder, newFolder.Name |
| 175 | + Next item |
| 176 | + |
| 177 | +End Sub |
| 178 | + |
| 179 | + |
| 180 | +Private Sub InsertCollectionValueAlphabetically(Col As Collection, item As Variant, Key As String) |
| 181 | +'Collections do not have a built in sort feature. This loops through each item in the collection, |
| 182 | +' and once the new item (key) comes later than the current loop value (Col(i).Name), then it |
| 183 | +' immediately exits the loop and adds the Key into that spot. |
| 184 | + |
| 185 | + Dim i As Long |
| 186 | + If Col.Count = 0 Then |
| 187 | + Col.Add item, Key 'First value gets added without trying to loop through |
| 188 | + Exit Sub |
| 189 | + End If |
| 190 | + |
| 191 | + For i = 1 To Col.Count |
| 192 | + 'Convert to lower case to get predictable behavior during ASCII text comparison |
| 193 | + If (LCase(Key) < LCase(Col(i).Name)) Then Exit For |
| 194 | + Next i |
| 195 | + |
| 196 | + If i = 1 Then |
| 197 | + 'Trying to add after index 0 results in an error |
| 198 | + Col.Add item, Key, 1 |
| 199 | + Else |
| 200 | + Col.Add item, Key, , i - 1 |
| 201 | + End If |
| 202 | +End Sub |
| 203 | + |
| 204 | + |
| 205 | +Private Function FormatFilePath(InputPath As String) As String |
| 206 | +'If a folder, normalize the root directory file path to have a backslash at the end of it. |
| 207 | +' Otherwise, it is a file and should be left alone. |
| 208 | + FormatFilePath = InputPath |
| 209 | + If (GetAttr(InputPath) And vbDirectory) = vbDirectory Then |
| 210 | + isFile = False |
| 211 | + If Right(InputPath, 1) <> "\" Then FormatFilePath = InputPath & "\" |
| 212 | + ElseIf Len(Dir(InputPath, vbReadOnly Or vbHidden Or vbSystem Or vbDirectory)) > 0 Then |
| 213 | + isFile = True |
| 214 | + End If |
| 215 | + |
| 216 | +End Function |
0 commit comments