![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
We can write a simple program to demonstrate how you can use the Drives collection to obtain information about the drives on the system. Create a Standard EXE project and place a Text Box on the form. Set the Text Boxs Multiline and Locked properties to True and set its size to nearly fill the form. Put the code from Listing 14.1 in the Text Boxs Click event procedure. When you run the program, click on the Text Box. After a brief pause it will display a list of the systems drives and their total and free space. This is shown in Figure 14.1. I named this program DRIVESDEMO. Listing 14.1 Demonstrating the Drives collection. Private Sub Text1_Click() Dim fs, d, dc Dim msg As String Set fs = CreateObject(Scripting.FileSystemObject) Set dc = fs.Drives For Each d In dc msg = msg & Drive & d.Path If Not d.IsReady Then msg = msg & is not ready. & vbCrLf Else msg = msg & vbCrLf & Space(5) msg = msg & Total space: & FormatNumber(d.TotalSize) msg = msg & vbCrLf & Space(5) msg = msg & Free space: & FormatNumber(d.FreeSpace) msg = msg & vbCrLf End If Next Text1.Text = msg End Sub The Folder ObjectA Folder object represents a single folder, or subdirectory, on a drive. You use the objects methods to copy, move, or delete the folder (as explained later) and the objects properties to obtain information about the folder. Perhaps most important, a Folder object contains two collections, Files and SubFolders, that provide access to the files and subfolders within the folder. Table 14.3 explains the properties of the Folder object. Because each Folder object contains information about its parent folder and its subfolders, you can easily traverse the entire folder structure on a drive. This is a powerful tool, as will be demonstrated later. First, however, we need to look at the Folder objects methods. The Copy method copies the folder and its contents to a new location. The syntax is (assuming f to be a Folder object) f.Copy destination[, overwrite] where destination specifies the destination where the folder is to be copied. Set overwrite to True (the default) to overwrite existing files or folders, or to False otherwise. Note that you can also copy a folder using the FileSystemObjects CopyFolder method. The Move method moves the folder and its contents from one location to another. The syntax is f.Move destination where destination specifies the destination where the folder is to be moved. You can also move folders with the FileSystemObjects MoveFolder method.
The Delete method deletes a folder and its contents. The syntax is: f.Delete [force] The optional force argument specifies whether files or folders with the read-only attribute are to be deleted (force = True) or not (force = False, the default). You can also delete folders with the FileSystemObjects DeleteFolder method. A Folder Demonstration To demonstrate the power of the Folder object, I have created a small utility that counts the total number of files and folders on your C: drive. This might seem like a difficult task, but as youll see, it requires relatively little code. Figure 14.2 shows the program in operation. The ease with which we can write this program is a result of two things: the design of the Folder object and the use of a recursive algorithm. Heres an outline of how it works:
|
![]() |
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. |