Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File counting routine in VB6 1

Status
Not open for further replies.

wmg

Technical User
Joined
Sep 13, 2001
Messages
216
Location
NZ
Hello!

I am writing a program where I need to know the number of all files and folders contained underneath a user specified folder.

Short of recursing and couting, is there any way I can do this more efficiently (like maybe hooking into the Windows Folder properties dialog and grabbing the number of files and folders from there??).

Any ideas?
wmg
 
I guess you could do that, but it's more complicated, and just looping through the files using the WIN32_FIND_DATA stucture and the associated functions might be just as fast (or faster).
Anyway, I wouldn't know a procedure to count the number of files at once. May somebody else...

Remedy
 
I have used the FileSystemObject for file quantities:

Set fs = CreateObject("scripting.filesystemobject")
Set f = fs.GetFolder(Label6.Caption)
Set fc = f.Files
If fc.Count > 0 Then
 
Thanks for that Klick - I'll give it a go. That code looks more like VBS code - will it work in normal-old VB6?

Regards
wmg
 
Yes, it'll work in "normal-old" VB6. Use Project|References to add "Microsoft Scripting Runtime" (SCRRUN.DLL). Here is the excerpt from MSDN:

FileSystemObject provides a non-hierarchical structure to manipulate, read, and create ASCII and Unicode text files. This structure is very different from the hierarchical structure of the original implementation of File I/O in Visual Basic. FileSystemObject does not support binary file access, so you must still use the original File I/O model in Visual Basic for binary file access.

MORE INFORMATION
FileSystemObject can be found in Scrrun.dll. In addition to FileSystemOject, Scrrun.dll includes four other objects available for File I/O and other tasks. These objects include the File object, the TextStreamObject object, the Folder object, and the Drive object. All of these objects have properties and methods that are detailed in the Help files.

You can obtain Scrrun.dll by installing one of the following packages:

Windows Script Host
Windows NT Option Pack
Microsoft Internet Information Server 3.0
Scripting 3.1 upgrade
Visual Studio 6.0
Visual Basic 6.0

FileSystemObject was originally created for the Visual Basic Scripting Edition. FileSystemObject is not included in the object library for Visual Basic or Visual Basic for Applications. To use FileSystemObject, you must select the Microsoft Scripting Run-time in the Project References dialog box for your project.

The following sample illustrates how to implement some of the FileSystemObject functionality. For more information, please see the Visual Basic Help files and the Visual Basic Books Online.

 
Excellent - thanks for your prompt response. I have integrated that counting mechanism into my project. Thanks!

I've also noticed that the rest of the program is built using seemingly 'old' ways of doing things. I might spend some time updating the code to use the filesystemobject throughout the rest of it.

Obviously, I'm assuming that the filesystemobject way will be more efficient some how.......I could be wrong though!

Regards
wmg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top