Browse DevX
Sign up for e-mail newsletters from DevX


FileSystemObject: Overhauled Feature Improves File Operations

With the release of Visual Basic Scripting Edition, version 2.0, Microsoft introduced the FileSystemObject. This object, and other objects in this group, encapsulate all the file operations that previously have been difficult or impossible to implement.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

ver since Visual Basic was first released, most of its major features have been overhauled at least once. The exception to this rule is in file operations. Just as in the original basic language, files are still opened using numbers. You can pass the numbers around through your code, but you always have to use somewhat obscure statements to write data to files.

With the release of Visual Basic Scripting Edition, version 2.0, Microsoft introduced the FileSystemObject. This is the overhaul that developers who work with disk files have been waiting for. This object, and other objects in this group, encapsulate all the file operations that previously have been difficult or impossible to implement.

To use these objects, you must reference the Microsoft Scripting Runtime (C:\Windows\System\SCRRUN.DLL) in your Visual Basic application. If you're using VBScript with Internet Explorer, the objects are available already. The objects of interest are these:

Drive. Represents a single storage device, including floppy disk, hard drive, CD-ROM, and more.
Drives. A collection of all the drives in the system.
File. Represents a single disk file somewhere on the system.
Files. A collection of disk files, typically located in a particular directory.
FileSystemObject. An object to represent the entire file system on your computer, including all drives, directories, and files.
Folder. Represents a single directory, whether local or network.
Folders. A collection of directories, which can be located at either the root level or in another directory.
TextStream. Represents a file that has been opened for reading or writing of data.

To get information on all the drives on your system, you could use the following code:

Sub Main() Dim objFSO As New Scripting.FileSystemObject Dim drvLoop As Scripting.Drive For Each drvLoop In objFSO.Drives Debug.Print drvLoop.DriveLetter & ":\" If drvLoop.DriveType = Fixed _ Or drvLoop.IsReady Then Debug.Print " Total size: " & Format$(drvLoop.TotalSize / (1024 ^ 2), "#0.00 Mb") Debug.Print " Free space: " & Format$(drvLoop.FreeSpace / (1024 ^ 2), "#0.00 Mb") Debug.Print " Volume Label: " & drvLoop.VolumeName Else Debug.Print " Disk information unavailable" End If Next drvLoop Set objFSO = Nothing End Sub

Be careful when attempting to access removable drives, especially if you are on a laptop with a swappable drive. Windows still thinks the drive is there in some cases. This can cause some ugly lockups of Visual Basic if you're not careful.

More properties are available for the Drive object—use the Object Browser to see all the available properties. You can also see online documentation at the Microsoft scripting technologies site.

Besides looping through drives, you can also loop through files in folders. This is especially helpful when you need to search your entire drive for a particular file. Recursion is also helpful in these cases, since you need to keep performing the same action as you work your way down the tree. The following code will count the number of files that have the .GIF extension. This code can obviously be adapted to list the files, do something to each file, and so on. The main point is the actual traversal of the directory tree.

Option Explicit Dim m_lngFileCount As Long Dim m_objFSO As Scripting.FileSystemObject Sub Main() Set m_objFSO = New Scripting.FileSystemObject m_lngFileCount = 0 CheckFolder "C:\" Debug.Print "Total files: " & m_lngFileCount End Sub Sub CheckFolder(strPath As String) Dim objFolder As Scripting.Folder Dim objFile As Scripting.File Dim objSubdirs As Scripting.Folders Dim objLoopFolder As Scripting.Folder Debug.Print "Checking directory " & strPath Set objFolder = m_objFSO.GetFolder(strPath) ' ' Check files in this directory ' For Each objFile In objFolder.Files If UCase$(Right$(objFile.ShortPath, 4)) = ".GIF" Then m_lngFileCount = m_lngFileCount + 1 End If Next objFile ' ' Loop through all subdirectories and ' do the same thing. ' Set objSubdirs = objFolder.SubFolders For Each objLoopFolder In objSubdirs CheckFolder objLoopFolder.Path Next objLoopFolder Set objSubdirs = Nothing Set objFolder = Nothing End Sub

To simplify the code, the FileSystemObject and the file counter are made global to this module (hence the m_ prefix). The CheckFolder routine is first called with C:\ as the starting point. This routine first goes through all the files in the selected directory. If any of them match, the counter is incremented. After all the files have been checked, each subdirectory is fed into the CheckFolder routine. This is the recursive part of the code—a routine is calling itself. Eventually, there are no more subdirectories and all the CheckFolder calls end. The total is then printed by Sub Main.

If you ever built this code using the Dir function, you should appreciate the simplicity of this version. No longer do you have to check file attributes or try to keep track of where you are at in the recursion. The new file objects take care of the dirty work for you.

Eric Smith has been a senior consultant with Andersen Consulting, specializing in client/server development using Oracle and Visual Basic, and was senior content developer (Visual Basic) at inquiry.com before it was purchased by Fawcette Technical Publications. He joined the Information Strategies team in September 1997 and is now an independent consultant. He has written or contributed to "Visual Basic 6 Bible" (IDG Books Worldwide, 1998), "Using Visual Basic 6" (Que), and "Inside VBScript with ActiveX" (New Riders Press, 1997). He is certified in Visual Basic 5.0 and Windows Architecture (1 and 2). Reach him here.
Thanks for your registration, follow us on our social networks to keep up-to-date