14 04 WWT3RANBTXQOTYJJPXCKFMIFL5MEPZBJ2BYRWNI




Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:File Management
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




I will have more to say about recursive algorithms later in the chapter when I demonstrate the file-related controls. Now let’s 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 Label’s 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 form’s 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 Button’s Click event procedure. When the user clicks on Start, this code does the following:

1.  Initializes the counter variables to zero.
2.  Disables the Command Buttons so they cannot be clicked while the counting process is in progress.
3.  Displays “Working …” in the Label control.
4.  Calls the procedure CountFilesAndFolders (yet to be written) to perform the count.
5.  When execution returns from the procedure, re-enables the Command Buttons.

Listing 14.2 The Command Button’s 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. Here’s what the code does:

1.  Creates a FileSystemObject.
2.  Loops through the Drives collection until drive C: is found.
3.  Passes the drive’s RootFolder, which you’ll remember is a Folder object, to the procedure DoCount.

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, here’s what it does:

1.  Gets the number of subfolders in the folder and adds it to the folders count.
2.  Gets the number of files in the folder and adds it to the files count.
3.  Calls DoCount for each subfolder in the Folders collection.

Here is where the recursion occurs—when the DoCount procedure calls itself. As this program illustrates, recursion can be a powerful technique for certain tasks. If you don’t 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 Object
In 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 FileSystemObject’s 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.

TIP:  Relative Vs. Absolute Paths
An absolute path specifies complete information about the name and location of a file, including drive letter. For example, c:\data\ sales\march1998.dat is an absolute path. A relative path specifies a file location relative to the current default path. Sales\ march1998.dat is a relative path, specifying that the file MARCH1998.DAT in the subfolder Sales is the current default folder. If, and only if, the default path is c:\data will the absolute and relative paths given here be equivalent.



Another way to obtain a File object is from a Folder object’s 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 application’s 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 object’s methods for certain types of file manipulation. Let’s look at the methods first, listed in Table 14.4. The File object’s properties are listed in Table 14.5. Finally, the Attributes property provides information about the file’s 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.



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.



Wyszukiwarka