![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
Finding FilesTo 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:
If flag = 2, FileAttr returns the files operating-system handle. Object-Oriented File ManagementThe 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:
Here is a brief outline of how the FSO system works:
Now lets get to the details. The Drives Collection And Drive ObjectsThe 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.
|
![]() |
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. |