![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
I will have more to say about recursive algorithms later in the chapter when I demonstrate the file-related controls. Now lets get started on the demo. Create a standard EXE project with a single form. Place one Label control and a control array of two Command Buttons on the form. Set the Labels Caption property to a blank string, and the Command Buttons Caption properties to Start and Quit. Place the following global variable declarations in the General section of the forms code: Dim NumFolders As Long, NumFiles As Long These variables will hold the counts of files and folders. Generally speaking, using global variables, such as these, is not good programming practice; for such a simple program, however, it makes our job easier and will not cause any problems. Next, place the code shown in Listing 14.2 in the Command Buttons Click event procedure. When the user clicks on Start, this code does the following:
Listing 14.2 The Command Buttons Click event procedure. Private Sub Command1_Click(Index As Integer) Select Case Index Case 0 Start NumFolders = 0 NumFiles = 0 Command1(0).Enabled = False Command1(1).Enabled = False Label1 = Working ... DoEvents Call CountFilesAndFolders Command1(0).Enabled = True Command1(1).Enabled = True Case 1 Quit End End Select End Sub The process of counting the files and folders is performed by the procedure CountFilesAndFolders. To be more accurate, the counting process is started by this procedure. The code is shown in Listing 14.3. Use the Tools|Add Procedure to create this procedure, then type in the code as shown. Heres what the code does:
So far so good, but you may have noticed that no counting has yet been done. That is accomplished in the DoCount procedure, which is our next task. Listing 14.3 The CountFilesAndFolders procedure. Public Sub CountFilesAndFolders() Dim fs, d Set fs = CreateObject(Scripting.FileSystemObject) Get drive C. For Each d In fs.Drives If d.DriveLetter = C Then Exit For Next Call DoCount(d.RootFolder) Label1 = Drive C has & NumFiles _ & files in & NumFolders _ & folders. End Sub The real work of the program is done by the DoCount procedure, shown in Listing 14.4. Given how much work it does, it is deceptively short. It is here that the power of the recursive algorithm comes into play. DoCount is passed a Folder object as its argument. Then, heres what it does:
Here is where the recursion occurswhen the DoCount procedure calls itself. As this program illustrates, recursion can be a powerful technique for certain tasks. If you dont believe me, try to write a program that counts the files and folders on a drive without using recursion. We will make good use of recursion again later in the chapter. Listing 14.4 The DoCount procedure. Public Sub DoCount(f As Folder) Dim f1 As Folder NumFolders = NumFolders + f.SubFolders.Count NumFiles = NumFiles + f.Files.Count For Each f1 In f.SubFolders Call DoCount(f1) Next End Sub The File ObjectIn the FSO model, each file on a disk is represented by a File object. This object has methods and properties that you use to get information about the file and manipulate it. First, however, you need to create a File object associated with a specific disk file. Creating A File Object There are two ways to create a File object. If you know the name and path of the file, you can use the FileSystemObjects GetFile method. The code is as follows, assuming that fs is an instance of the FileSystemObject class: Dim f Set f = fs.GetFile(filespec) The filespec argument is the relative or absolute path to the file. An error occurs if filespec does not exist. Note that executing GetFile does not open the file or do anything else to it, but simply returns a File object linked to the file.
Another way to obtain a File object is from a Folder objects Files collection. As you learned earlier in this chapter, you can create a Folder object for any subfolder on a disk, and the Folder object contains a Files collection containing one File object for each file in the folder. You can write code to iterate through the collection, looking for one or more files that meet a specified criterion. For example, the following code creates an array of File objects containing all the DLL files in the applications current path: Dim fs, f, f1, filelist() Dim i As Integer, j As Integer i = 0 ReDim filelist(0) Set fs = CreateObject(Scripting.FileSystemObject) Set f = fs.GetFolder(App.Path) For Each f1 In f.Files If LCase(Right(f1.Name, 3)) = dll Then i = i + 1 ReDim Preserve filelist(i) Set filelist(i) = f1 End If Next If i > 0 Then For j = 1 To i Debug.Print filelist(j).Name Next End If Working With File Objects Once you create a File object, you have access to all the properties of the file. You can also use the objects methods for certain types of file manipulation. Lets look at the methods first, listed in Table 14.4. The File objects properties are listed in Table 14.5. Finally, the Attributes property provides information about the files attributes, such as whether it is read-only or a system file. The single value returned by the Attributes property is a composite of the individual attribute values, as shown in Table 14.6. This table also indicates which of the individual attributes are read/write and which 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. |