Results 1 to 2 of 2

Thread: displaying files

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    25

    displaying files

    Hello,

    I havent' really worked with files. In my program, I want to have a label, and a command button. When I click the command button, I want the label to show me files on my hard drive like continiously. u know that effect on how when your searching for a file, and it searches all the folders, displaying its name?? Kinda like that. like how anti virus programs search your files fora virus, and display the name of the file and the directory its in...

    ok im an idiot. just help please. thanks.

    p.s. i just need the names to be displayed.. nothing fancy.

    muchas gracias.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You'll need to use a Recursive function, you can do this using the Dir() function, API (as in my example) or using the FileSystemObject.

    In a Module
    VB Code:
    1. Option Explicit
    2.  
    3. 'API Consts, Types and Functions
    4. Private Const MAX_PATH = 260
    5. Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
    6.  
    7. Private Type FILETIME
    8.         dwLowDateTime As Long
    9.         dwHighDateTime As Long
    10. End Type
    11.  
    12. Private Type WIN32_FIND_DATA
    13.         dwFileAttributes As Long
    14.         ftCreationTime As FILETIME
    15.         ftLastAccessTime As FILETIME
    16.         ftLastWriteTime As FILETIME
    17.         nFileSizeHigh As Long
    18.         nFileSizeLow As Long
    19.         dwReserved0 As Long
    20.         dwReserved1 As Long
    21.         cFileName As String * MAX_PATH
    22.         cAlternate As String * 14
    23. End Type
    24.  
    25. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    26. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    27. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    28. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    29.  
    30. Private aFileList As Variant
    31. Private lFileCount As Long
    32.  
    33. Public Function FastFindFiles(ByVal sFolder As String, Optional ByVal sPattern As String = "*", Optional ByRef Display As Object) As Variant
    34.     'Initialize the Private File Array and Count, then call the Fast
    35.     'File Recursive Function to populate the Array, then return it.
    36.     lFileCount = 0
    37.     aFileList = Array()
    38.     Screen.MousePointer = vbArrowHourglass
    39.     Call RecurseFindFiles(sFolder, sPattern, Display)
    40.     Screen.MousePointer = vbDefault
    41.     FastFindFiles = aFileList
    42. End Function
    43.  
    44. Private Sub RecurseFindFiles(ByVal sFolder As String, ByVal sPattern As String, Optional ByRef Display As Object)
    45.     Dim tFD As WIN32_FIND_DATA
    46.     Dim lFile As Long
    47.     Dim bFound As Long
    48.     Dim aSubs() As String
    49.     Dim lSubs As Long
    50.     Dim sFilename As String
    51.    
    52.     'Make sure the passed folder includes an ending "\"
    53.     If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
    54.    
    55.     'Find the First File in the Specified Location
    56.     lFile = FindFirstFile(sFolder & "*", tFD)
    57.     bFound = lFile
    58.    
    59.     'Loop while a File is found
    60.     While bFound
    61.         'Get the Filename
    62.         sFilename = UCase(Left(tFD.cFileName, InStr(tFD.cFileName, Chr(0)) - 1))
    63.        
    64.         ' If a Display Control was passed, update it.
    65.         If Not Display Is Nothing Then
    66.             Display = ShortenPath(sFolder) & sFilename
    67.             Display.Refresh
    68.         End If
    69.        
    70.         If (tFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
    71.             'If it's a Folder, add it to the Sub Folders Array
    72.             If Left(sFilename, 1) <> "." Then
    73.                 ReDim Preserve aSubs(lSubs)
    74.                 aSubs(lSubs) = sFilename
    75.                 lSubs = lSubs + 1
    76.             End If
    77.         Else
    78.             'If it's a File, compare it to the Pattern for a Match
    79.             If sFilename Like UCase(sPattern) Then
    80.                 'If it matches, add it to the File Array
    81.                 ReDim Preserve aFileList(lFileCount)
    82.                 aFileList(lFileCount) = sFolder & sFilename
    83.                 lFileCount = lFileCount + 1
    84.             End If
    85.         End If
    86.         'Find the Next File, (if there is one).
    87.         bFound = FindNextFile(lFile, tFD)
    88.     Wend
    89.     'Close the API Find Handle
    90.     Call FindClose(lFile)
    91.    
    92.     'If there were Sub Folders found, Recurse them too..
    93.     If lSubs Then
    94.         For lSubs = 0 To UBound(aSubs)
    95.             Call RecurseFindFiles(sFolder & aSubs(lSubs), sPattern, Display)
    96.         Next
    97.     End If
    98. End Sub
    99.  
    100. Private Function ShortenPath(ByVal sPath As String) As String
    101.     ' Convert a long path into a shortened version using Ellipses
    102.     Dim sFolder As String
    103.     Dim lPos As Long
    104.    
    105.     ShortenPath = sPath
    106.     If Len(sPath) < 30 Then Exit Function
    107.     sFolder = Mid(sPath, InStrRev(sPath, "\", Len(sPath) - 1) + 1)
    108.     Do While InStr(lPos + 1, sPath, "\") < 30
    109.         lPos = InStr(lPos + 1, sPath, "\")
    110.     Loop
    111.     ShortenPath = Left(sPath, lPos) & "...\" & sFolder
    112. End Function
    Example Usage:
    VB Code:
    1. Private Sub cmdFind_Click()
    2.     Dim vFiles As Variant
    3.     Dim nFile As Long
    4.    
    5.     vFiles = FastFindFiles("C:\", txtFind, Label1)
    6.     List1.Clear
    7.     For nFile = 0 To UBound(vFiles)
    8.         List1.AddItem vFiles(nFile)
    9.     Next
    10. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width