|
32 | 32 |
|
33 | 33 | DirectoryManager is simple and fast to set up. The below examples will walk you through common use. They are also located in the [example workbook](/ExampleWorkbook.xlsm).
|
34 | 34 |
|
35 |
| -## Initial Setup, Printing All Files/Folders |
| 35 | +## Initial Setup, List All Files and Folders in a Custom Directory |
36 | 36 |
|
37 | 37 | The first step is to declare the variable and initialize it with a path. The path can be to either a folder or a file.
|
38 | 38 |
|
@@ -149,6 +149,90 @@ Sub CheckIfFileOrFolderExists()
|
149 | 149 | End Sub
|
150 | 150 | ```
|
151 | 151 |
|
| 152 | +## Print a List of All Folders and Files |
| 153 | + |
| 154 | +Using a helper method, we can recursively iterate through every file and folder in the DirectoryManager. This example prints the results to the Immediate Window. |
| 155 | + |
| 156 | +```VBA |
| 157 | +Sub PrintFilesAndFolders(Directory As DirectoryManager, Optional indent As String) |
| 158 | +'Helper method |
| 159 | +
|
| 160 | + Dim folder As DirectoryManager |
| 161 | + Dim file As DirectoryManager |
| 162 | + Dim newIndent As String |
| 163 | + |
| 164 | + For Each folder In Directory.Folders |
| 165 | + Debug.Print indent & "+ " & folder.Name |
| 166 | + newIndent = indent & " " |
| 167 | + PrintFilesAndFolders folder, newIndent |
| 168 | + Next folder |
| 169 | + |
| 170 | + For Each file In Directory.Files |
| 171 | + Debug.Print indent & "- " & file.Name |
| 172 | + Next file |
| 173 | + |
| 174 | +End Sub |
| 175 | +
|
| 176 | +Sub LoopThroughAllFilesAndFolders() |
| 177 | +
|
| 178 | + Dim dm As DirectoryManager |
| 179 | + |
| 180 | + Set dm = New DirectoryManager |
| 181 | + dm.Path = ThisWorkbook.Path & "\Sample Data Set" |
| 182 | + |
| 183 | + PrintFilesAndFolders dm |
| 184 | + |
| 185 | + 'Output from above: |
| 186 | + |
| 187 | +' + _My Personal Documents |
| 188 | +' - Document 1.txt |
| 189 | +' - Document 2.txt |
| 190 | +' - Document 3.txt |
| 191 | +' + Contacts |
| 192 | +' + _Emergency Contacts |
| 193 | +' - Father.txt |
| 194 | +' - Mother.txt |
| 195 | +' - Sibling.txt |
| 196 | +' - Spouse.txt |
| 197 | +' + _Personal Contacts |
| 198 | +' - Contact 1.txt |
| 199 | +' - Contact 2.txt |
| 200 | +' - Contact 3.txt |
| 201 | +' + Business Contacts |
| 202 | +' - _Contact 1A.txt |
| 203 | +' - _Contact 2A.txt |
| 204 | +' - Contact 1.txt |
| 205 | +' - Contact 2.txt |
| 206 | +' - Contact 3.txt |
| 207 | +' - Contact 4.txt |
| 208 | +' - Contact 5.txt |
| 209 | +' - Contact 6.txt |
| 210 | +' - Contact 7.txt |
| 211 | +' - _My Old Phone.txt |
| 212 | +' - My Phone.txt |
| 213 | +' + Documents |
| 214 | +' - Document 1.txt |
| 215 | +' - Document 2.txt |
| 216 | +' - Document 3.txt |
| 217 | +' - Document 4.txt |
| 218 | +' - Document 5.txt |
| 219 | +' - Document 6.txt |
| 220 | +' + My Publications |
| 221 | +' - Publication 1.txt |
| 222 | +' - Publication 2.txt |
| 223 | +' - Publication 3.txt |
| 224 | +' - Publication 4.txt |
| 225 | +' - Publication 5.txt |
| 226 | +' + Pictures |
| 227 | +' - _Sample File A.txt |
| 228 | +' - Sample File 1.txt |
| 229 | +' - Sample File 2.txt |
| 230 | +' - Sample File 3.txt |
| 231 | + |
| 232 | +End Sub |
| 233 | +``` |
| 234 | + |
| 235 | + |
152 | 236 | # Contributing and Outlook
|
153 | 237 |
|
154 | 238 | I am not actively pursuing additional development. This Class resource has all intended functionality in version 1.0. I consider it feature complete, but will continue to provide bug support.
|
|
0 commit comments