FileSystemObject: Overhauled Feature Improves File Operations

FileSystemObject: Overhauled Feature Improves File Operations

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:WindowsSystemSCRRUN.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 ExplicitDim m_lngFileCount As LongDim m_objFSO As Scripting.FileSystemObjectSub Main()   Set m_objFSO = New Scripting.FileSystemObject   m_lngFileCount = 0   CheckFolder "C:"   Debug.Print "Total files: " & m_lngFileCountEnd SubSub 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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes