EziData Solutions - Brisbane's Access Specialists
Working with Drives, Folders and Files Tutorials

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.

'declare the starting or root folder – you could get this from the Drives

Dim strRoot As String

strRoot = "C:\"

'declare the variables for use with the Scripting library

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


'get the folder from fso

Set parent = fso.GetFolder(strRoot)


'get the subfolders contained within the root folder

Set children = parent.SubFolders

'iterate through the subfolder under the root and display some data

For Each child In children

'iterate through the files

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.

'declare the starting or root folder – you could get this from the Drives

Dim strFilePath As String

strFilePath = "C:\Temp\MyWorld.txt"

'declare the variables for use with the Scripting library

Dim fso As New Scripting.FileSystemObject

Dim fl As File


'get the file from fso

Set fl = fso.GetFile(strFilePath)


'display some information about the file

Debug.Print fl.Name & " " & fl.Type & " " & fl.Size

'TODO: add some code to move, copy or delete the file

'you can even open it as a TextStream to read the contents

Other Topics about Using the FileSystemObject

Getting Information About Your Drives

Getting Information About Your Folders

Working with Text Files

Reading from Text Files