In VB6, you often need to implement file search facilities in your applications. For instance, you might need to search for a particular log file on the client machine, get a listing of all files available in the particular directory, or verify that a particular directory or file exists. The simplest way to achieve this is to employ the FileSystemObject component, which provides a useful set of features that can simplify coding.

FileSystemObject component
The FileSystemObject gives you access to the file system. It allows creating, manipulating, deleting, and obtaining information about drives, folders, and files. To use FileSystemObject in your VB code, you need to declare it in the following way:
Dim fso As New FileSystemObject

You can then use its properties and methods, which are shown in Table A.
Table A



FileSystemObject Main object Allows creating, deleting, manipulating, and getting information about drives, folders, files
Drive Object Allows getting information about a drive
Drives Collection Lists drives available on the system (both physical and logical)
File Object Allows creating, deleting, and moving files, as well as getting file properties
Files Collection Lists all files in a given folder
Folder Object Allows creating, moving, and deleting folders, as well as getting information about a particular folder
Folders Collection Provides a list of all folders in a Folder
TextStream Object Allows reading and writing text files

FileSystemObject object model

Table B outlines the most often used methods of the FileSystemObject.
Table B




Creates a new folder
fso.CreateFolder “C:\Files”
Creates a specified filename and returns a TextStream object that can be used to read from or write to the file
fso.CreateTextFile “C:\Files\file1.txt”
Deletes folder
fso.DeleteFolder “C:\Files”
Deletes file
fso.DeleteFile “C:\Files.file.txt”
Copies a folder and its contents to another folder
fso.CopyFolder “C:\Files”, “C:\Files_Copy”
Copies a file to another location
fso.CopyFile “C:\Files\file.txt”, “C:\Files\file_copy.txt”
Moves a folder to another location
fso.MoveFolder “C:\Files”, “C:\Files Move”
Moves a file to another location
fso.MoveFile “C:\Files\file.txt”, “C:\Files\file_move.txt”
Returns a Drive object corresponding to the drive in a specified path. Allows getting various information about the drive, such as available space, drive letter, drive type, file system, free space, serial number, share name, total size
fso.GetDrive(“C”) AvailableSpace
Checks whether a drive exists
If fso.DriveExists(“D”) Then
    MsgBox “Drive D found”

End If

Returns a Folder object corresponding to the folder in a specified path
Returns a string containing the name of the parent folder of the last component in a specified path fso.GetParentFolderName(app.Path)
Returns the special folder specified. One of three options: WindowsFolder (contains files installed by the Windows operating system), SystemFolder (contains fonts, libraries, device drivers), or TemporaryFolder (used to store temp files)
Returns a File object corresponding to the file in a specified path .GetFile “C:\Files\file.txt”
Checks whether a folder exists
If fso.FolderExists(“C:\Files”) Then
    MsgBox “Folder Exists!”
End If

Checks whether a file exists
If fso.FileExists(“C:\Files\file.txt”) Then
    MsgBox “File Exists!”
End if

Common FileSystemObject methods

Code example
Let’s take a look at the code that will allow you to go through files in a specified directory and list them in a Listbox control. We’ll assume that you have a Listbox control on a form called lstFiles and a C:\Files directory on your system that contains a number of files. Listing A shows the code to retrieve a list of those files.

FileSystemObject limitations
When you implement FileSystemObject, you should keep in mind these limitations:

  • ·        FileSystemObject allows creating ASCII or Unicode text files.
  • ·        FileSystemObject can read only ASCII text files.
  • ·        When reading files, FileSystemObject can read in only one direction and only line by line.
  • ·        A file can’t be open for reading and writing. You can open a file in ForReading mode using OpenTextFile, but to make changes to the file, you would need to open a TextStream object.
  • ·        When searching for files, you can’t use wildcards; you must specify the exact name.
  • ·        When retrieving filenames from the files collection, filenames can’t be sorted. To return them in a specific order, you must add a subroutine to do it.

In this article, we demonstrated a simple way to implement a file search, copy, move, and delete files and folders, and other useful features using FileSystemObject. Next time, we will look at other code examples that will simplify your code.