Displaying File Listings on a ListView Control (Article)

Category:
Files and Directories
Type:
Snippets
Difficulty:
Beginning

Author: David Nishimoto

Version Compatibility: Visual Basic 6

More information:
This article explains how to use the File System Object to get a list of files, each file's size, type, and data modified attributes; and display the file information in a list view control.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Combining File System Object and List View Control

Overview:This article explains how to use
the File System Object to get a list of files, each
file's size, type, and data modified attributes; and display
the file information in a list view control.

Setup:Make sure to include the
"Windows Scripting Runtime" in the references
section of your project. txtDirectory
is a textbox to input a directory path. ListViews
is a list view control to display file information.
cmdQuery is a command button to 
query and build a file list to be displayed in
the list view control.

Option Explicit

1. Define List View Column Headings

The List View view type is lvwReport which allows multicolumn data to be displayed in the List View Control. Each columnHeader is created by invoking the Add method of the ColumnHeaders collection of the List View Control. Private Sub Form_Load() Dim objColumnHeader As ColumnHeader listViewFiles.View = lvwReport listViewFiles.ColumnHeaders.Clear Set objColumnHeader = listViewFiles.ColumnHeaders.Add() objColumnHeader.Text = "Name" objColumnHeader.Width = 2000 Set objColumnHeader = listViewFiles.ColumnHeaders.Add() objColumnHeader.Text = "Size" objColumnHeader.Width = 1000 Set objColumnHeader = listViewFiles.ColumnHeaders.Add() objColumnHeader.Text = "Type" objColumnHeader.Width = 1000 Set objColumnHeader = listViewFiles.ColumnHeaders.Add() objColumnHeader.Text = "Modified" objColumnHeader.Width = 2000 End Sub

2. Build the File List

Create a dynamically sizing array called Files() and populate it with file names for a directory.

3. Insert File Information

The Structure for the List View Control is ColumnHeader which display Column Headings for each data column, ListItems which represents the rows, and ListSubItems which reference data in each column and is a child object to the ListItems. The Number of ListSubItems is always one less than the number of ColumnHeaderfirst ColumnHeader object is associated with the text property of the ListItem. Inserting data or creating a List View Row is done by calling listViewFiles.ListItems.Add which create a new ListItem object to the ListItems collection. objListItem.Text = Files(i) assigns text to the first column. Data for the remaining columns is accomplished by creating a ListSubItems object. where Set objItemCell = objListItem.ListSubItems.Add creates a ListSubItems object. Private Sub cmdQuery_Click() Dim Files() As String Dim i As Integer Dim objListItem As ListItem Dim objItemCell As ListSubItem Dim lSize As Long If txtDirectory = "" Then MsgBox "Please Enter in a Directory" Exit Sub End If Call ListFiles(txtDirectory.Text, Files()) listViewFiles.ListItems.Clear For i = 0 To UBound(Files) - 1 'File Name Set objListItem = listViewFiles.ListItems.Add objListItem.Text = Files(i) 'Size lSize = CLng(GetFileAttribute(txtDirectory & "/" & Files(i), "Size")) Set objItemCell = objListItem.ListSubItems.Add objItemCell.Text = CStr(lSize) 'Type Set objItemCell = objListItem.ListSubItems.Add objItemCell.Text = GetFileAttribute(txtDirectory & "/" & Files(i), "Type") 'Modified Set objItemCell = objListItem.ListSubItems.Add objItemCell.Text = GetFileAttribute(txtDirectory & "/" & Files(i), "DateLastModified") Next End Sub
Private Function GetFileAttribute(sFileName As String, sAttribute As String) As String Dim objFile As Object Dim sRC As String Dim fs As New FileSystemObject Dim sAttributes As String If sFileName <> "" Then Set objFile = fs.GetFile(sFileName) Select Case sAttribute Case "DateLastAccessed" sRC = objFile.DateLastAccessed Case "DateLastModified" sRC = objFile.DateLastModified Case "DateCreated" sRC = objFile.DateCreated Case "Drive" sRC = objFile.Drive Case "Size" sRC = objFile.Size Case "Type" sRC = objFile.Type Case "Path" sRC = objFile.Path Case "ParentFolder" sRC = objFile.ParentFolder Case "Attributes" sAttributes = "" 'Normal If objFile.Attributes And 0 = 0 Then sAttributes = sAttributes & "N" End If 'ReadOnly If objFile.Attributes And 1 = 1 Then sAttributes = sAttributes & "R" End If 'Hidden If objFile.Attributes And 2 = 2 Then sAttributes = sAttributes & "H" End If 'System If objFile.Attributes And 4 = 4 Then sAttributes = sAttributes & "S" End If 'Volume If objFile.Attributes And 8 = 8 Then sAttributes = sAttributes & "V" End If 'Directory If objFile.Attributes And 16 = 16 Then sAttributes = sAttributes & "D" End If 'Archive If objFile.Attributes And 32 = 32 Then sAttributes = sAttributes & "A" End If 'Alias If objFile.Attributes And 64 = 64 Then sAttributes = sAttributes & "l" End If 'Compressed If objFile.Attributes And 128 = 128 Then sAttributes = sAttributes & "C" End If sRC = sAttributes End Select End If GetFileAttribute = sRC End Function
Public Sub ListFiles(sFolderName As String, ByRef sFileNames() As String) Dim iCount As Integer Dim sTemp As String Dim objFolder As Object Dim objFile Dim i As Integer Dim fs As New FileSystemObject iCount = 0 'Set the folder object Set objFolder = fs.GetFolder(sFolderName) iCount = objFolder.Files.Count 'Redimension an string array to hold the filenames ReDim sFileNames(iCount) As String i = 0 For Each objFile In objFolder.Files sFileNames(i) = objFile.Name i = i + 1 Next End Sub