Getting Information about your Files
Having come this far, with a complete list of Drives and Folders, the next logical step is to see if we can find out anything useful about what Files our Folders contain. Thankfully once again the FileSystemObject contains a very useful method called Files that returns a collection of files within the current folder.
Building on the example we used when dealing with Folders using the FileSystemObject, we can now gather a raft of information about the files located in a particular folder using the Files collection of the FileSystemObject.
Dim strRoot As String
strRoot = "C:\"
Dim fso As New Scripting.FileSystemObject
Dim parent As Scripting.Folder
Dim children As Scripting.Folders
Dim child As Scripting.Folder
Dim fl As File
Set parent = fso.GetFolder(strRoot)
Set children = parent.SubFolders
For Each child In children
For Each fl In child.Files
Debug.Print fl.Name & " " & fl.Type
Next
Next
Of course, sometimes you already know the location of the file you want information about, so the FileSystemObject includes another function called GetFile that takes as its parameter the full path and filename of the file.
Dim strFilePath As String
strFilePath = "C:\Temp\MyWorld.txt"
Dim fso As New Scripting.FileSystemObject
Dim fl As File
Set fl = fso.GetFile(strFilePath)
Debug.Print fl.Name & " " & fl.Type & " " & fl.Size
Other Topics about Using the FileSystemObject
Getting Information About Your Drives
Getting Information About Your Folders
Working with Text Files
Reading from Text Files
|