December 03, 2020, 06:48:29 am
News: GalahTech Community Based Technical Support!
Pages: [1]   Go Down
Author Topic: Fixed Topic Name  (Read 517 times)

Stars: +0/-0
Offline Offline

Gender: Male
Topic Necromancer
Posts: 4668

I code C#!

« on: April 08, 2003, 11:09:57 am »


Does anyone know an \'easy\' way to loop through a dir with multiple subdirs and add all dirs and files into a listview.
So basically I want to import the directory structure into a listview.. Just like the Windows Explorer.

I have searched on VBF, but didn\'t find a good answer..




Stars: +0/-0
Offline Offline

Posts: 21

« Reply #1 on: April 08, 2003, 01:24:39 pm »

There are dozens of examples on how to recursively parse thru directories on sites like Planet-Source-Code.

You can \"cheat\" a bit by using the FileList & DirList stock controls in VB and transfer the contents to your listview.

Just some ideas...

Master of TMEOotAP

Stars: +0/-0
Offline Offline

Posts: 10349

« Reply #2 on: April 23, 2003, 12:21:54 pm »

Here is a routine from that uses a Treeview instead of a listview.[vbcode]\' DirView sample from
\' Written by Matt Hart
\' Copyright 1999 by Matt Hart
\' This software is FREEWARE. You may use it as you see fit for
\' your own projects but you may not re-sell the original or the
\' source code. Do not copy this sample to a collection, such as
\' a CD-ROM archive. You may link directly to the original sample
\' using \"\"
\' No warranty express or implied, is given as to the use of this
\' program. Use at your own risk.
\' This shows how to populate a TreeView control with a directory
\' and sub-directory listing.  It uses a recursive FindDirs call.
\' I\'ve modified this to use the FindFirstFile API call rather than
\' VB\'s built in Dir$ call.  Dir$ will return the first Directory
\' it finds, but subsequent Dir$ calls return both files and directories,
\' so you must use FileAttr to test for directories - very slow, because
\' you must do 2 API calls (Dir$ and FileAttr) while one API call can
\' do both for you.

Private 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

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

Private Type DirInfo
    DirName     As String
End Type

Private bCancel As Boolean

Private Sub FindDirs(D$, T As TreeView)
    Static bFirstIn As Boolean
    If bCancel Then Exit Sub
    Dim nx As Node, C$
    Dim N As Integer, Srch$, i As Integer, NewD$
    C$ = D$
    If Right$(C$, 1) [] \"\" Then C$ = C$ & \"\"
    If Not bFirstIn Then
        bFirstIn = True
        Set nx = T.Nodes.Add(, , C$, C$)
    End If
    Srch$ = C$ & \"*.*\"
    ReDim Dees(1 To 10) As DirInfo
    Call LoadDirs(Dees(), N, Srch$)
    If N Then
        For i = 1 To N
            Set nx = T.Nodes.Add(C$, 4, Dees(i).DirName, LastPath$(Left$(Dees(i).DirName, Len(Dees(i).DirName) - 1)))
        Exit Sub
    End If
    For i = 1 To N
        NewD$ = RTrim$(Dees(i).DirName)
        Call FindDirs(NewD$, T)
End Sub

Private Function LastPath$(P$)
    Dim i
    For i = Len(P$) To 1 Step -1
        If Mid$(P$, i, 1) = \"\" Then
            LastPath$ = Mid$(P$, i + 1)
            Exit For
        End If
End Function

Private Sub LoadDirs(D() As DirInfo, N As Integer, Srch$)
    Dim a$, Max As Integer, i As Integer, k As Integer, W32 As WIN32_FIND_DATA, fHandle As Long, lResult As Long
    Dim oPath$
    Max = UBound(D)
    N = 0
    oPath$ = Left$(Srch$, Len(Srch$) - Len(LastPath$(Srch$)))
    fHandle = FindFirstFile(Srch$, W32)

    If fHandle Then
            a$ = Left$(W32.cFileName, InStr(W32.cFileName, Chr$(0)) - 1)
            If a$ [] \".\" And a$ [] \"..\" And ((W32.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) ] 0) Then
                N = N + 1
                If Max [ N Then
                    Max = Max + 10
                    ReDim Preserve D(1 To Max) As DirInfo
                End If
                D(N).DirName = oPath$ & a$ & \"\"
            End If
            If bCancel Then Exit Do
            lResult = FindNextFile(fHandle, W32)
        Loop While lResult
        lResult = FindClose(fHandle)
    End If
    If bCancel Then Exit Sub

    For i = 1 To N - 1
        For k = i + 1 To N
            If UCase$(D(i).DirName) ] UCase$(D(k).DirName) Then
                a$ = D(k).DirName
                D(k).DirName = D(i).DirName
                D(i).DirName = a$
            End If
End Sub

Private Sub cmdLoadEm_Click()
    Static Done
    If Done Then Exit Sub
    Done = True
    bCancel = False
    Command1.Caption = \"Cancel\"
    Call FindDirs(\"C:\", TV)
    Command1.Caption = \"Fill It!\"
    MsgBox \"Done!\"
    Done = False
End Sub[/vbcode]


Pages: [1]   Go Up
Jump to: