Describing a resource producer VB Help

When about recursive procdure, it’s heipful to start with a genral written dflCnpticr ot the-procedure. For the FilcScan application, we need a subroutine (since it’s not going to return a result) that scans the contents of a folder: let’s call it Scanfolderst). The ScanFoldersO subroutine must scan all the entries of the initial folder and process the files in it. If the current entry is a file, it must act upon the file, depending on its name, size, or any of its attributes. If the current entry is a folder, it must scan the contents of this folder. In other words, it must interrupt the scanning of the current folder and start scanning the sub folder. And the most efficient way to scan a subfolder is to have it to call itself. Here’s the ScanFolders() function in pseudocode.

Translating the Description to Code

Now, let’s translate this description to actual code. Because we need access to each folder’s files and subfolders, we’ll use a DriveListBox control, a DirListBox control, and a FileListBox control, as shown in Figure 11.6.Using these controls, we can enumerate the subfolders of any given folder with the DirListBox control and its files with the FileListBox control.

Using the File Controls

The three file controls (DriveListBox, DirListBox, and FileListBox) are connected with the following subroutines.

When the user selects a different drive from the DriveListBox control, the program switches to the new drive and updates the contents of the Dirl1stBox control. Likewise, when the user selects a folder in the DirListBox control, the program switches to the selected folder and updates the contents of the FileListBox.

Since we are only scanning folders, we don’t need to display the FileListBox control on the Form, but I decided to include it so that you can watch the progress of the scanning process.
The sub folders of a given folder are displayed in a DirListBox control (Dirl) and are accessed with the help of the Dirl.List array. The first subfolder is Dirl.List(O), the second subfolder is Dir1.List(l), and so on, Similarly, you can access the files of the current folder through the Filel.ListO array. After the user selects the folder to be scanned in the DirListBox control and clicks the Scan Now button, the program starts scanning.

The global variable InitialFolder is used later in the code to determine whether we have exhausted all the sUbfolders of the current folder and must end the process.

The ScanFoldersO Subroutine

Now,let’s look at the actual code of the ScanFolden() subroutine. The number of subfolders in the current folder is given by.the number of entries in the DtrListBox control, which is 01 .LiItCount. U this number is positive, the program scaN each lubfolder with a For Next loop. With each iteration, it switchel to th next folder, updates the contents of the DirListBox and PUeLlatBox controll, and can. itself to scan the new folder.
This process is repeated as long as there are subfolders to be scanned. When a ” folder without sub folders is reached, the If statement is skipped, and the Scan- ” FoldersO subroutine moves to the parent folder and returns. The ScanFolders() subroutine doesn’t do any real processing. It simply counts the total number of
files in the folders visited with the following statement:

totalFiles – total Files + Filel.ListCount

If you tun the FileSCan appHcation, you can watch the folder names in the Dirty ListBox control and the current folder’s files in the FileLi~tBox control. In a real application, you won’t want to display the name of the folder visited and its files, so these controls should be ii’lVisible.But if you’re trying to leam how the program operates, it might be helpful to watch the folder  process through the Changing contents of theaf , 0 control! on the Form.

Building a Custom Expiorer

FileScan is an interesting example of recursive programming, but why duplicate functionality that’s already available for free? All the features of the FileScan application are built into Wmdows Explorer. One reason is that the FileScan application is highly customizable. In the previous section, you learned how to count all the files of a given folder, including those in its subfolders. You can add many more useful features to the FileScan application that aren’t available through Wmdows Explorer.
For example, you can implement a version of the Find utility that locates files and/ or folders based on criteria that aren’t available through the Find utility. A limitation of the Find utility is that you can’t specify exclusion criteria. For instance, you can’t ask it to find all the files whose size exceeds 1MB that aren’t system files (e.g., EXE, DLL) or images (e.g., BMP,TIP, JPG). But with FileScan, you can modify the application to handle alltypes of file selection or rejection criteria by designing the proper user interface.

VB6 at Work: The DirMap Projed

Here’s another customization idea for the FileScan application: Have you ever had to prepare a hard copy of your hard disk’s structure? (If you ever have to submit the contents and structure of an entire CD to a publisher, this utility will save you a good deal of work.) As far as I know, there is no simple way to do it. However, you can easily modify the FileScan application so-that it prints the contents of a folder, including its subfolders, to a textbox. Figure 11.7 shows the DirMap application, which does exactly that. The structure of a user-specified folder is printed on a RichTextBox control so that folder names can be displayed in bold and stand out. The contents of the textbox can be copied and pasted in any other document or used in a mail message. The code of the DirMap application is quite similar to the code of the FileScan application, with the exception of a few additional lines that create the output shown in the lower half of the Form

Let’s examine the code of the DirMap application. The DirStructure variable is a long string variable that holds the RTFdescription of the contents of the RichTextBox control. This string is slowly built as the program scans each folder along the way. Folder names are displayed in bold and filenames are displayed in regular font and indented from the right according to their level within the parent folder. The \b tag in RTF causes everything that appears within a pair of turly braces to appear in bold. Folder names are printed as {\b c:\windows\desktopJ, and when rendered in the RichTextBox control, they appear in bold.

Bve time this subroutine visits a new folder, it prints the names of all the files in it with the first For Next loop. Likewise, every time it switches to anotber  it prints its name in bold with the following line:

Notice that the C&$(lO)+ChrS(13) combination won’t cause a line bleak in a Rich· TextSox control. ‘J’heDoubleSluhes() function rep1acet the .luba in th path name with two sluhet. The-sluh ts a .pedal character in RTF; to cause th RichTextBox control to print a .lash instead 01interpreting a stuh, you must prefix the slash with another .lash.

Posted on November 6, 2015 in Recursive Programming

Share the Story

Back to Top