|
|
Title | Search for files matching a pattern and show their sizes and total size |
Description | This example shows how to search for files matching a pattern and show their sizes and total size in Visual Basic 6. This example uses the FindFirstFile, FindNextFile, and FindClose API functions. |
Keywords | file, size, search, pattern |
Categories | Files and Directories, Utilities, API |
|
|
The program uses the FindFirstFile, FindNextFile, and FindClose API functions to find files matching the pattern. It adds each file's name and size to a ListBox and keeps track of the total size.
|
|
Private total_size As Double
' List all files below the directory that
' match the pattern.
Private Sub ListFiles(ByVal start_dir As String, ByVal _
pattern As String, ByVal lst As ListBox)
Const MAXDWORD = 2 ^ 32
Dim dir_names() As String
Dim num_dirs As Integer
Dim i As Integer
Dim fname As String
Dim attr As Integer
Dim search_handle As Long
Dim file_data As WIN32_FIND_DATA
Dim file_size As Double
' Get the matching files in this directory.
' Get the first file.
search_handle = FindFirstFile( _
start_dir & pattern, file_data)
If search_handle <> INVALID_HANDLE_VALUE Then
' Get the rest of the files.
Do
fname = file_data.cFileName
fname = Left$(fname, InStr(fname, Chr$(0)) - 1)
file_size = (file_data.nFileSizeHigh * _
MAXDWORD) + file_data.nFileSizeLow
If file_size > 0 Then
lst.AddItem start_dir & fname & " (" & _
Format$(file_size) & ")"
total_size = total_size + file_size
Else
lst.AddItem start_dir & fname
End If
' Get the next file.
If FindNextFile(search_handle, file_data) = 0 _
Then Exit Do
Loop
' Close the file search hanlde.
FindClose search_handle
End If
' Get the list of subdirectories.
search_handle = FindFirstFile( _
start_dir & "*.*", file_data)
If search_handle <> INVALID_HANDLE_VALUE Then
' Get the rest of the files.
Do
If file_data.dwFileAttributes And DDL_DIRECTORY _
Then
fname = file_data.cFileName
fname = Left$(fname, InStr(fname, Chr$(0)) _
- 1)
If fname <> "." And fname <> ".." Then
num_dirs = num_dirs + 1
ReDim Preserve dir_names(1 To num_dirs)
dir_names(num_dirs) = fname
End If
End If
' Get the next file.
If FindNextFile(search_handle, file_data) = 0 _
Then Exit Do
Loop
' Close the file search handle.
FindClose search_handle
End If
' Search the subdirectories.
For i = 1 To num_dirs
ListFiles start_dir & dir_names(i) & "\", pattern, _
lst
Next i
End Sub
|
|
If you double-click a file in the ListBox, the program uses the ShellExecut API function to open the file.
|
|
' Open the double-clicked file.
Private Sub lstFiles_DblClick()
Dim file_name As String
Dim pos As Integer
' Remove the file's size.
file_name = lstFiles.Text
pos = InStrRev(file_name, " (")
If pos > 0 Then file_name = Left$(file_name, pos - 1)
ShellExecute Me.hwnd, "open", file_name, _
vbNullString, vbNullString, SW_SHOWNORMAL
End Sub
|
|
|
|
|
|