dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB6
Expertise: Intermediate
Apr 21, 2001

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


FilesToArray - Read all the files or subdirectories in a directory using API functions

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
    (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
    (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As _
    Long
Const MAX_PATH = 260

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type

' Returns a one-based string array containing the files (or directories)
' located in the specified path
'
' the Path argument can contain wildcards, e.g. "C:\*.doc")

Function FilesToArray(ByVal Path As String, Optional ByVal IncludeDirs As _
    Boolean) As String()
    Dim lRet As Long
    Dim handle As Long
    Dim FindData As WIN32_FIND_DATA
    Dim FileName As String
    Dim fileCount As Long
    Dim ok As Boolean
    ReDim res(0) As String
    
    ' start the searching, exit if no file matches the spec
    handle = FindFirstFile(Path, FindData)
    If handle < 0 Then
        FilesToArray = res()
        Exit Function
    End If
        
    Do
        ' get this entry's name
        FileName = Left$(FindData.cFileName, InStr(FindData.cFileName, _
            vbNullChar) - 1)
        
        If (FindData.dwFileAttributes And vbDirectory) = 0 Then
            ' this is a file
            ok = Not IncludeDirs
        ElseIf FileName <> "." And FileName <> ".." Then
            ' this is a directory, but not a ./.. entry
            ok = IncludeDirs
        Else
            ' this is a ./.. entry
            ok = False
        End If
            
        If ok Then
            ' add this entry to the result
            fileCount = fileCount + 1
            If fileCount > UBound(res) Then
                ' make room in the array if necessary
                ReDim Preserve res(fileCount + 100) As String
            End If
            res(fileCount) = FileName
        End If
        ' read the next file, returns zero when there are no more files
        lRet = FindNextFile(handle, FindData)
    Loop While lRet

    ' stop enumeration
    FindClose handle
    
    ' discard unused array items and return to caller
    ReDim Preserve res(0 To fileCount) As String
    FilesToArray = res
End Function

Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date