Results 1 to 23 of 23

Thread: [Resolved]Display directory contents in a listview

  1. #1

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160

    Arrow [Resolved]Display directory contents in a listview

    Im having problem using Listview control...in my application, i want to display all files in a particular directory...let say i only want to display a directory Dir1.Path = "C:\ISISArchive"...and then i want my listview to view all files in this directory...

    can someone show me a code for this???
    ur help would be much appreciated!!!
    Last edited by nyah_RMC; Nov 12th, 2002 at 09:49 PM.
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  2. #2
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    The quickest way of loading a ListBox is:
    VB Code:
    1. Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long,  ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    2. Private Sub Command1_Click()
    3.     Dim r As Long
    4.     r = SendMessageStr(List1.hwnd, &H18D, &H20, "C:\*.*")
    5. End Sub
    But to have more control of the individual files (so you can also add sizes and dates etc to your ListView) you might want to take some of these examples and combine them:

    To check if a file exists:
    VB Code:
    1. Function FileExist(ByVal strPath As String) As Boolean
    2.     FileExist = Len(Dir$(strPath)) <> 0
    To check if a Directory exists:
    VB Code:
    1. Function DirExist(ByVal DirPath As String) As Boolean
    2.     On Error Resume Next
    3.     'The above line is only required on Win NT4, and 2K. Remove on Win9x
    4.     DirExist = Len(Dir$(DirPath, vbDirectory)) <> 0
    5. End Function
    To check to see if strFile is a directory or not:
    VB Code:
    1. If (GetAttr(strPath & “\” & strFile) AND vbDirectory) = vbDirectory then
    2. ‘ it’s a directory, not a file
    To load an array with the names of all the files in a directory (not sub-directories):
    VB Code:
    1. Function DirList(strDir As String) As String()
    2.     'returns a 0 based array with the files in strDir
    3.     'Can contain a pattern such as "*.*"
    4.     Dim Count As Integer
    5.     Dim sFiles() As String
    6.     Dim sFile As String
    7.    
    8.     sFile = Dir$(strDir)
    9.     Count = -1
    10.     Do
    11.         Count = Count + 1
    12.         ReDim Preserve sFiles(Count)
    13.         sFiles(Count) = sFile
    14.         sFile = Dir$
    15.     Loop
    16.     DirList = sFiles
    17. End Function

  3. #3
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    If you are having problems with the ListView parts, then this might help:


    The following code is used in order to show to the user the list of files in a folder.

    Notes:
    · The display of the list of files is done using the ListView control, rather than the standard and simple List control. The ListView allows for multiple columns, user re-sizing of the columns, sorting by columns etc.
    · The ListView control is used by adding “Microsoft Windows Common Controls” to the project (Project Components).
    · In order to use this control, the MSCOMCTL.OCX file is required in C:\Windows\System.

    Add a ListView into the project. Modify it as follows:
    · View = 3 - Report
    · Border Style = 1
    · Column Headers:
    · Name – Width of 3800
    · Extn – 700
    · Date – 1800
    · Size – 600 Right Align
    · SizeFormatted – 0 Right Align
    · DateFormatted – 0 Right Align
    · Font = MS Sans Serif of 8.25

    Loop through the files in a folder using this code. The variable f will hold just the file names found, not the full path to the file.
    VB Code:
    1. SearchName = gFolder & "\*.*"
    2.     f = Dir(SearchName, vbReadOnly)
    3.     While f <> ""
    4.             f = Dir()
    5.     Wend

    Before adding items into the ListView, it is first cleared out of any old information:
    ListView1.ListItems.Clear

    For each file found (i.e. for each f) within the list of files loop above, the details are added into the ListView using:
    VB Code:
    1. fdate = FileDateTime(gFolder & "\" & f)
    2.     fsize = FileLen(gFolder & "\" & f
    3.     Set ItmX = ListView1.ListItems.Add(, , f)
    4.       ItmX.SubItems(1) = UCase(thisExtn)
    5.       ItmX.SubItems(2) = ddate
    6.       ItmX.SubItems(3) = Round(fsize / 1024, 1)
    7.       ItmX.SubItems(4) = Format(fsize), "0000000000")
    8.       ItmX.SubItems(5) = Format(ddate, "YYYYMMDDHhNnSs")
    9.     Set ItmX = Nothing
    Note: To allow the correct sorting fields, the last two items take the size and date and ensure these items can be sorted correctly. However, these sorting columns are described with a width of 0, so the user never sees them.

    The ListView can be sorted with:
    VB Code:
    1. Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)
    2.     If ColumnHeader.Text = "Size" Then
    3.         ListView1.SortKey = 4
    4.     ElseIf ColumnHeader.Text = "Date" Then
    5.             ListView1.SortKey = 5
    6.     Else
    7.         ListView1.SortKey = ColumnHeader.Index - 1
    8.     End If
    9.     ListView1.SortOrder = Abs(Not ListView1.SortOrder = 1)
    10.     ListView1.Sorted = True
    11. End Sub

  4. #4

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    thanks for your codes, but i only want to put several codes on my Form_load event.. i want my listview control to display upon form load my specified file directory files...i adopt ur code like this:

    VB Code:
    1. Private Sub Form_Load()
    2. Dim searchname, f, gfolder  As String
    3. searchname = gfolder & "C:\ISISArchive"
    4.     f = Dir(searchname, vbReadOnly)
    5.     While f <> ""
    6.             f = Dir()
    7.     Wend
    8.     ListView1.ListItems.Clear
    9.     fdate = FileDateTime(gfolder & "C:\ISISArchive" & f)
    10.     fsize = FileLen(gfolder & "C:\ISISArchive" & f)
    11.     Set ItmX = ListView1.ListItems.Add(, , f)
    12.     ItmX.SubItems(1) = UCase(thisExtn)
    13.     ItmX.SubItems(2) = ddate
    14.     ItmX.SubItems(3) = Round(fsize / 1024, 1)
    15.     ItmX.SubItems(4) = Format(fsize, "0000000000")
    16.     ItmX.SubItems(5) = Format(ddate, "YYYYMMDDHhNnSs")
    17.     Set ItmX = Nothing
    18.  End Sub

    but it gives me an "Invalid property value" error, and it points on this particular line

    VB Code:
    1. ItmX.SubItems(1) = UCase(thisExtn)

    do i have to decare ItmX??? what could be the problem here???
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  5. #5
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    thisExtn is the type of the file (.DOC, .XLS, .INI etc).
    It can be got by:

    thisExtn = Right(f,3)

    You can always add a:
    Dim ItmX
    if you need to.

    But how many columns have you added to your ListView?

  6. #6

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    i didnt add for columns. i found this code here somewhere in the forum and it works in a listbox control.

    VB Code:
    1. Private Sub Form_Load()
    2. Dim strFile As String
    3.     strFile = Dir("C:\ISISArchive\*.*")
    4.     While Not Len(strFile) = 0
    5.        List1.AddItem strFile
    6.        strFile = Dir()
    7.     Wend
    8. End Sub

    however, i want to use a listview control for the headers (file name, file size and date modified..etc..)...but how can i do it???
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  7. #7
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Try this out and let me know:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim i%
    5.  
    6.     With ListView1
    7.         .ColumnHeaders.Add , , "File Name"
    8.         .ColumnHeaders.Add , , "Size (KB)"
    9.         .ColumnHeaders.Add , , "Date/Time"
    10.         '.ColumnHeaders.Add , , "Sub Item 3"
    11.         '.ColumnHeaders.Add , , "Sub Item 4"
    12.         For i = 1 To .ColumnHeaders.Count
    13.             .ColumnHeaders(i).Width = .Width / .ColumnHeaders.Count
    14.         Next i
    15.     End With
    16.     CheckFolder "C:\Windows"
    17.  
    18. End Sub
    19.  
    20. Public Sub CheckFolder(strFolder As String, Optional strExt As String = "")
    21. '===========================================================================
    22. Dim strFile As String
    23. Dim intPos As Integer
    24. Dim vListItem As ListItem
    25. Dim AddToList As Boolean
    26.  
    27. On Error GoTo ErrClear
    28.  
    29.     If Not Right(strFolder, 1) = "\" Then strFolder = strFolder & "\"
    30.     strFile = Dir(strFolder, vbNormal)
    31.     Do While strFile <> ""
    32.         If strFile <> "." And strFile <> ".." Then
    33.             If (GetAttr(strFolder & strFile) And vbNormal) = vbNormal Then
    34.                 If Not Trim(strExt) = "" Then
    35.                     intPos = InStrRev(strFile, ".")
    36.                     If LCase(Mid(strFile, intPos + 1)) = strExt Then
    37.                         AddToList = True
    38.                     End If
    39.                 Else
    40.                     AddToList = True
    41.                 End If
    42.                 If AddToList = True Then
    43.                     Set vListItem = ListView1.ListItems.Add(, , strFile)
    44.                     'you may want to format the following outputs
    45.                     vListItem.SubItems(1) = Format(FileLen(strFolder & strFile) / 1024, "#######.00")
    46.                     vListItem.SubItems(2) = FileDateTime(strFolder & strFile)
    47.                     'vListItem.SubItems(3) = FileLen(strFolder & strFile)
    48.                     'vListItem.SubItems(4) = FileDateTime(strFolder & strFile)
    49.                 End If
    50.             End If
    51.         End If
    52.         strFile = Dir
    53.         AddToList = False
    54.     Loop
    55.     Exit Sub
    56.    
    57. ErrClear:
    58. '-----------
    59.     Err.Clear
    60.     Resume Next
    61.  
    62. End Sub
    Roy

  8. #8

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    ey roy it works! though jordanchris' code gives a lot of ideas! but this is just what i need roy! THANKS!...but one more thing roy...it didnt display the file size and the date just the file name..maybe there's missing here..
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  9. #9
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    hmm, it does?!
    VB Code:
    1. '.....................
    2.     vListItem.SubItems(1) = Format(FileLen(strFolder & strFile) / 1024, "#######.00")
    3.     vListItem.SubItems(2) = FileDateTime(strFolder & strFile)
    4. '.....................
    Roy

  10. #10

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    i dont know what's wrong with this, but it seems like it didnt read these lines...

    VB Code:
    1. '.....................
    2.     vListItem.SubItems(1) = Format(FileLen(strFolder & strFile) / 1024, "#######.00")
    3.     vListItem.SubItems(2) = FileDateTime(strFolder & strFile)
    4. '.....................

    i tried to change the listview properties but it didnt work! well i also noticed that the word Format(FileLen and FileDateTime is written in blue...but when i try to copy this code it is written in black...are these some vb function???
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  11. #11
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Oh, don't be silly - it's a forum, they have their own way of formatting key words. VB doesn't necessary have to look the same. But enough of lyrics ...

    I'm not sure what are doing in your project, but you need to set your Listview1.View = 3 (Report view) in order to see columns and headers.
    Roy

  12. #12

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    oh im sorry for finding my post 'silly'....i just thought that those WORDS are embedded VB functions...or just a user-defined VARIABLES!...anyways, ive already set the view property to 3 but still not working...THANKS FOR UR TIME! ...nyways, im adopting ur code here...maybe i just work it out myself!
    Last edited by nyah_RMC; Nov 11th, 2002 at 09:54 PM.
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  13. #13
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    What isn't working exactly? Can post your code?
    Roy

  14. #14

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    ok...i didnt change your posted code here except the dir path

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim i%
    5.  
    6.     With ListView1
    7.         .ColumnHeaders.Add , , "File Name"
    8.         .ColumnHeaders.Add , , "Size (KB)"
    9.         .ColumnHeaders.Add , , "Date Modified"
    10.         '.ColumnHeaders.Add , , "Sub Item 3"
    11.         '.ColumnHeaders.Add , , "Sub Item 4"
    12.         For i = 1 To .ColumnHeaders.Count
    13.             .ColumnHeaders(i).Width = .Width / .ColumnHeaders.Count
    14.         Next i
    15.     End With
    16.     CheckFolder "C:\ISISArchive"
    17.  
    18. End Sub
    19.  
    20. Public Sub CheckFolder(strFolder As String, Optional strExt As String = "")
    21. '===========================================================================
    22. Dim strFile As String
    23. Dim intPos As Integer
    24. Dim vListItem As ListItem
    25. Dim AddToList As Boolean
    26.  
    27. On Error GoTo ErrClear
    28.  
    29.     If Not Right(strFolder, 1) = "\" Then strFolder = strFolder & "\"
    30.     strFile = Dir(strFolder, vbNormal)
    31.     Do While strFile <> ""
    32.         If strFile <> "." And strFile <> ".." Then
    33.             If (GetAttr(strFolder & strFile) And vbNormal) = vbNormal Then
    34.                 If Not Trim(strExt) = "" Then
    35.                     intPos = InStrRev(strFile, ".")
    36.                     If LCase(Mid(strFile, intPos + 1)) = strExt Then
    37.                         AddToList = True
    38.                     End If
    39.                 Else
    40.                     AddToList = True
    41.                 End If
    42.                  If AddToList = True Then
    43.                     Set vListItem = ListView1.ListItems.Add(, , strFile)
    44.                     'you may want to format the following outputs
    45.                     vListItem.SubItems(1) = Format(FileLen(strFolder & strFile) / 1024, "#######.00")
    46.                     vListItem.SubItems(2) = FileDateTime(strFolder & strFile)
    47.            
    48.                 End If
    49.             End If
    50.         End If
    51.         strFile = Dir
    52.         AddToList = False
    53.     Loop
    54.     Exit Sub
    55.    
    56. ErrClear:
    57. '-----------
    58.     Err.Clear
    59.     Resume Next
    60.  
    61. End Sub

    and here is my Listview1 properties:

    .View = 3
    .Arrange = 0
    .LabelEdit = 0
    .BorderStyle = 1
    .Appearance = 1
    .OLEDragMode = 0
    .OLEDropNone = 0

    and i checked the following:
    HideSelection
    LabelWrap
    Enabled

    i also did not add any columns either...there...hope we can figure out now

    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  15. #15
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Looks fine, so what's not working for you? I'm confused.
    Roy

  16. #16

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    AHEM...like i said..the listview control didnt display the size and the date..it only displays the FileNAME...i attached my listview snapshot here..
    Attached Images Attached Images  
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  17. #17
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Debug time....
    Put a breakpoint on the line:
    vListItem.SubItems(2) = FileDateTime(strFolder & strFile)

    When the program stops, check to see what the following are set to:
    strFolder
    strFile
    vListItem.SubItems(1)

  18. #18

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    I tried to change some codes here and it displays the size and date of the LAST file...(see my uploaded image)...how can i display all file's sizes and date modifications????

    VB Code:
    1. Public Sub CheckFolder(strFolder As String, Optional strExt As String = "")
    2. '===========================================================================
    3. Dim strFile As String
    4. Dim intPos As Integer
    5. Dim vListItem As ListItem
    6. Dim AddToList As Boolean
    7.  
    8. On Error GoTo ErrClear
    9.  
    10.     If Not Right(strFolder, 1) = "\" Then strFolder = strFolder & "\"
    11.     strFile = Dir(strFolder, vbNormal)
    12.     Do While strFile <> ""
    13.         If strFile <> "." And strFile <> ".." Then
    14.             If (GetAttr(strFolder & strFile) And vbNormal) = vbNormal Then
    15.                 If Not Trim(strExt) = "" Then
    16.                     intPos = InStrRev(strFile, ".")
    17.                     If LCase(Mid(strFile, intPos + 1)) = strExt Then
    18.                         AddToList = True
    19.                     End If
    20.                 Else
    21.                     AddToList = True
    22.                 End If
    23.                  If AddToList = True Then
    24.                     Set vListItem = ListView1.ListItems.Add(, , strFile)
    25.                     ListView1.ListItems(ListView1.ListItems.Count).SubItems(2) = _
    26.                     CStr(FileDateTime(strFolder + "\" + strFile))
    27.                     ListView1.ListItems(ListView1.ListItems.Count).SubItems(1) = _
    28.                     CStr(FileLen(strFolder + "\" + strFile))
    29.                  End If
    30.             End If
    31.         End If
    32.         strFile = Dir
    33.         AddToList = False
    34.     Loop
    35.     Exit Sub
    36.    
    37. ErrClear:
    38. '-----------
    39.     Err.Clear
    40.     Resume Next
    41.  
    42. End Sub
    Attached Images Attached Images  
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  19. #19

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    IT finally works! I just removed the sorting order on my listview property!! and thanks to those who reply on my post! u HELP A LOT GUYS!!! THANK YOU VERY MUCH!!!
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  20. #20
    Addicted Member
    Join Date
    Jun 2002
    Location
    Sunny Scottsdale Arizona
    Posts
    254
    nyah_RMC

    I have read this post and found it to be helpful. Have adapted your code to serve my purposes.

    Only thing I am unclear on is what you are accomplishing with this line of code:

    If (GetAttr(strFolder & strFile) And vbNormal) = vbNormal Then

    Thanks for the great example.

  21. #21

    Thread Starter
    Addicted Member nyah_RMC's Avatar
    Join Date
    Jan 2002
    Location
    citadel
    Posts
    160
    hey dude, it's been a while.... oops, i need to check my code and study it again ! (cause im shifted to doing web applications..)..nyways, i tried to eliminate that line of code in my apps and it still works..so you can also disregard it! (kinda dont remember why i put it anyway)
    robee

    There's nothing wrong in asking, it proves only one thing. That you are Ignorant! but willing to dig on facts!

  22. #22
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    It is probably there to only include "normal" files, not hidden or system files.

  23. #23
    Addicted Member
    Join Date
    Jun 2002
    Location
    Sunny Scottsdale Arizona
    Posts
    254
    makes sense. thnx

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