Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Finding Files

To obtain the name of the first file that matches a template, use the Dir function:

[filename = ] Dir[(template)]

The template argument is the file template you want to match. It can contain a drive specifier, path, and wildcard characters (as explained earlier in the chapter). The first time a program calls Dir, it must pass a template argument. Dir returns the name of the first file in the specified or current folder that matches template. If no match occurs, an empty string is returned. If Dir is called again one or more times with the template argument omitted, it returns the next file that matches the original template, or a null string if no other match exists.

The Dir function is typically used to see if a particular file exists. This is accomplished by calling Dir with the file name as the argument, then seeing if a null string is returned:

If Dir “MYFILE.TXT” = “” Then
    ‘ File does not exist.
Else
    ‘File exists.
End If

You can also use Dir to get a list of all files matching a template that includes wildcards. For example, the following code would load List Box List1 with the names of all files that have the .DAT extension and are located in the specified directory:

Dim s As String
s = Dir “C:\DATA\*.DAT”
Do While s <> “”
    List1.AddItem s
    s = Dir
Loop

To change the name of a file or folder, or to move a file to a different folder on the same drive, use the Name statement:

Name oldfilespec As newfilespec

The oldfilespec argument gives the name of the existing file or folder and can include a drive and path; newfilespec gives the new file or folder name and must refer to the same drive as oldfilespec. You cannot use Name with an open file.

To obtain the handle or mode of an open file, use the FileAttr function:

[result =] FileAttr(filenum, flag)

The filenum argument is the number associated with the file when it was opened; flag specifies the type of information returned by the function. If flag = 1, then FileAttr returns a code indicating the mode in which the file was opened:

Return Value File Mode
1 INPUT
2 OUTPUT
4 RANDOM
8 APPEND
32 BINARY

If flag = 2, FileAttr returns the file’s operating-system handle.

Object-Oriented File Management

The FSO approach to file access is based upon the FileSystemObject class. This approach is new with the current version of Visual Basic and provides capabilities for both file access and file management. FSO file access was covered in Chapter 13; here, we will examine FSO file management.

The first step is to create an instance of the FSO class. You can do it like this:

Dim fs
Set fs = CreateObject(“Scripting.FileSystemObject”)

Or like this:

Dim fs As New Scripting.FileSystemObject

Note the required use of the Scripting qualifier, which identifies the library that the FileSystemObject class is defined in. Once you have created an instance of the FileSystemObject class, you can use its properties and methods to perform various file ma-nipulation tasks.

The FileSystemObject is the “top” object in the FSO hierarchy, providing access to all of the drives (both local and network), folders, and files on the system. The hierarchy has several other objects, which correspond to the way in which disk drives are organized:

  Drive—Corresponds to a single disk drive on the system.
  Folder—Corresponds to a single folder (subdirectory) on a drive.
  File—Corresponds to a single file in a folder.

Here is a brief outline of how the FSO system works:

  The FileSystemObject has a Drives collection that contains a Drive object for each local and network drive on the system. You can query a Drive object’s properties to obtain information about the drive, such as its type and free space.
  Each Drive object contains a Folder object representing the top-level folder on the drive.
  Each Folder object contains a Folders collection and a Files collection. The Folders collection contains a Folder object for each subfolder, and the Files collection contains a File object for each file in the folder.

Now let’s get to the details.

The Drives Collection And Drive Objects

The one and only property of the FileSystemObject is the Drives collection, which contains all the Drive objects available on the system (including local drives and shared network drives). The Drives collection is like any other Visual Basic collection and is used the same way as you have learned in previous chapters.

Each Drive object has a set of properties that provides information about the physical drive. These properties are listed in Table 14.2. Except as noted, these properties are read-only.

Table 14.2 Drive object properties.

Property Description
AvailableSpace The amount of space available to a user on the specified drive or network share. Generally the same as the FreeSpace property, but may differ on systems that support quotas.
DriveLetter The drive letter associated with the drive. Returns a zero-length string for network drives that have not been mapped to a drive letter.
DriveType A value indicating the type of drive. Possible values are 0 (unknown), 1 (removable), 2 (fixed), 3 (network), 4 (CD-ROM), and 5 (RAM disk).
FileSystem The type of file system. Available return types include FAT, NTFS, and CDFS.
FreeSpace The amount of space available to a user on the specified drive or network share. Generally the same as the AvailableSpace property, but may differ on systems that support quotas.
IsReady Returns True if the drive is ready, False if not. Used with removable media and CD-ROM drives, returning False if the media (diskette) has not been inserted.
Path The path of the drive. This consists of the drive letter followed by a colon.
RootFolder Returns a Folder object representing the drive’s root path.
SerialNumber The unique serial number identifying a disk. Use this property to verify that a removable media drive contains the proper media.
ShareName The share name assigned to a network drive. For non-network drives, a zero-length string.
TotalSize The total capacity of the drive.
VolumeName The volume name of the drive. You can write to this property to change a drive’s volume name.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.