Visual Basic (VB and VBA)

Copyright 1999-2001 Christopher Greaves. All rights reserved.
Home Page and email to [email protected]
If in doubt, record a macro and inspect the entrails!

 

 

Please read the DISCLAIMER.

Here is an INDEX to all the procedures.

You will probably need one copy of my GLOBAL DECLARATIONS.



Public Function lngLoadList(frmMe As UserForm, lb As ListBox, strPath As String, strName As String, strExtent As String, boolRecurse As Boolean) As Long
' Procedure :   lngLoadList
' Description:  Load a listbox from a file/path/extent specification.
' Copyright:    Chris Greaves Inc.
' Inputs:       Userform, Listbox, Path, Name, Extent and recursion flag.
'               Userform - The form containing the listbox to be loaded.
'               Listbox - The listbox to be loaded.
'               Path - Path of parent directory, optinal trailing path separator.
'               Name - Generic name of files being sought; "RoR" will find "RoR*.*.
'               Extent - Generic extent of files being sought; "DO" will find "*.DOt" and "*.DOc".
'               Recursion flag - if TRUE we will search all child directories.
' Returns:      LONG count of items in the listbox.
' Assumes:      None.
' Side Effects: None.
' Tested:       By the calls shown below.
    ' Attach a trailing path separator ("\") if one is not present
    Dim strLocPath As String
    If Right(strPath, 1) = Application.PathSeparator Then
        strLocPath = strPath
    Else
        strLocPath = strPath & Application.PathSeparator
    End If
    Dim strFile As String ' holds successive results of the DIR operation - found files.
    Dim strDirs As String ' holds a string of directories at this level.
             frmMe.Caption = "Searching in " & strLocPath: frmMe.Repaint
    ' FIRST: Are we asked to search directories below this directory?
    If boolRecurse Then
        ' Prepare search for all directory files at this level.
        strFile = Dir(strLocPath & "*.*", vbDirectory)
        While strFile <> "" ' Until DIRECTORY search is exhausted.
            On Error GoTo Failed1
            If (GetAttr(strLocPath & strFile) And vbDirectory) = vbDirectory Then
                ' Ignore current and parent directories.
                If strFile <> strcExtentSeparator And strFile <> ".." Then
                    ' Append this directory to the list of directories at this level.
                    strDirs = strDirs & strLocPath & strFile & ","
                Else
                End If
            Else
            End If
Failed1:
        On Error GoTo 0
        strFile = Dir ' get next DIRECTORY result at this level
        Wend
    Else
    End If
    ' SECOND: Obtain all matching files at the current level.
    strFile = Dir(strLocPath & strName & "*." & strExtent & "*", vbNormal)
    While strFile <> "" ' Until FILE search is exhausted.
        On Error GoTo Failed2
        If (GetAttr(strLocPath & strFile) And vbDirectory) <> vbDirectory Then
            Call lngAddToListBox(lb, strLocPath & strFile)
        Else
        End If
Failed2:
        On Error GoTo 0
        strFile = Dir ' get next FILE result at this level
    Wend
    ' THIRD: Process all found directories at this path (recurse)
    While Len(strDirs) > 0
        Dim strThisDir As String
        strThisDir = strSplitStringAt(strDirs, Right(strDirs, 1), True) ' get next substring
        strDirs = strSplitStringAt(strDirs, Right(strDirs, 1), False) ' drop that substring
        Call lngLoadList(frmMe, lb, strThisDir, strName, strExtent, boolRecurse)
    Wend
    lngLoadList = lb.ListCount
End Function

We all knew nothing when we started …
Home Page and Contact Information
Send email to [email protected].
This page was last updated Thursday, November 15, 2001

 

 

Hosted by www.Geocities.ws

1

Visual Basic (VB and VBA)

Copyright 1999-2001 Christopher Greaves. All rights reserved.
Home Page and email to [email protected]
If in doubt, record a macro and inspect the entrails!

 

 

Please read the DISCLAIMER.

Here is an INDEX to all the procedures.

You will probably need one copy of my GLOBAL DECLARATIONS.

Public Function lngLoadList(frmMe As UserForm, lb As ListBox, strPath As String, strName As String, strExtent As String, boolRecurse As Boolean) As Long
' Procedure :   lngLoadList
' Description:  Load a listbox from a file/path/extent specification.
' Copyright:    Chris Greaves Inc.
' Inputs:       Userform, Listbox, Path, Name, Extent and recursion flag.
'               Userform - The form containing the listbox to be loaded.
'               Listbox - The listbox to be loaded.
'               Path - Path of parent directory, optinal trailing path separator.
'               Name - Generic name of files being sought; "RoR" will find "RoR*.*.
'               Extent - Generic extent of files being sought; "DO" will find "*.DOt" and "*.DOc".
'               Recursion flag - if TRUE we will search all child directories.
' Returns:      LONG count of items in the listbox.
' Assumes:      None.
' Side Effects: None.
' Tested:       By the calls shown below.
    ' Attach a trailing path separator ("\") if one is not present
    Dim strLocPath As String
    If Right(strPath, 1) = Application.PathSeparator Then
        strLocPath = strPath
    Else
        strLocPath = strPath & Application.PathSeparator
    End If
    Dim strFile As String ' holds successive results of the DIR operation - found files.
    Dim strDirs As String ' holds a string of directories at this level.
             frmMe.Caption = "Searching in " & strLocPath: frmMe.Repaint
    ' FIRST: Are we asked to search directories below this directory?
    If boolRecurse Then
        ' Prepare search for all directory files at this level.
        strFile = Dir(strLocPath & "*.*", vbDirectory)
        While strFile <> "" ' Until DIRECTORY search is exhausted.
            On Error GoTo Failed1
            If (GetAttr(strLocPath & strFile) And vbDirectory) = vbDirectory Then
                ' Ignore current and parent directories.
                If strFile <> strcExtentSeparator And strFile <> ".." Then
                    ' Append this directory to the list of directories at this level.
                    strDirs = strDirs & strLocPath & strFile & ","
                Else
                End If
            Else
            End If
Failed1:
        On Error GoTo 0
        strFile = Dir ' get next DIRECTORY result at this level
        Wend
    Else
    End If
    ' SECOND: Obtain all matching files at the current level.
    strFile = Dir(strLocPath & strName & "*." & strExtent & "*", vbNormal)
    While strFile <> "" ' Until FILE search is exhausted.
        On Error GoTo Failed2
        If (GetAttr(strLocPath & strFile) And vbDirectory) <> vbDirectory Then
            Call lngAddToListBox(lb, strLocPath & strFile)
        Else
        End If
Failed2:
        On Error GoTo 0
        strFile = Dir ' get next FILE result at this level
    Wend
    ' THIRD: Process all found directories at this path (recurse)
    While Len(strDirs) > 0
        Dim strThisDir As String
        strThisDir = strSplitStringAt(strDirs, Right(strDirs, 1), True) ' get next substring
        strDirs = strSplitStringAt(strDirs, Right(strDirs, 1), False) ' drop that substring
        Call lngLoadList(frmMe, lb, strThisDir, strName, strExtent, boolRecurse)
    Wend
    lngLoadList = lb.ListCount
End Function

We all knew nothing when we started …
Home Page and Contact Information
Send email to [email protected].
This page was last updated Thursday, November 15, 2001

 

 

Hosted by www.Geocities.ws

1