Is there a way to use FileSystemObjects to put all directories into a List View control, then take a directory the user doubleclicks on, and grab all subdirectories inside it?
Printable View
Is there a way to use FileSystemObjects to put all directories into a List View control, then take a directory the user doubleclicks on, and grab all subdirectories inside it?
I undestand that you want to load a Listview control with directories located in let's say your C:\ root directory, plus you want to do something when user DblClicks on some folder. That's clear, but what the hell does this mean???
"... and grab all subdirectories inside it?"
It means I want to compile a list of all the subdirectories inside the directory on which the user clicks. The program is very close to what I want. Here's an example, hopefully it'll help you determine what I want.
Ok, let's say I have a root folder. The root folder has 3 subfolders: Windows, Program Files, and Temp. I want the code to be refined to where it lists only those folders, and when I doubleclick on one of those folders, it gets all the folder's subfolders and puts double dots as the first item in the Listview, indicating you can go back one directory.
Here's the code I want refined:
Code:Private Function GetFolders(sStartFolder As String)
On Error Resume Next
If status = False Then Exit Function
Dim fso As New FileSystemObject
Dim f As Folder
Dim fldr As Folder
Set fldr = fso.GetFolder(sStartFolder)
GetFolders = fldr.Path & vbCrLf
Debug.Print fldr
x = x + 1
Set Litem = ListView1.ListItems.Add(, , x, , 1)
Litem.SubItems(1) = fldr
Litem.SubItems(2) = "-"
Litem.SubItems(3) = Format(fldr.Size, "##,##0.00")
Litem.SubItems(4) = fldr.Type
GetFiles (fldr)
If fldr.SubFolders.Count > 0 Then
For Each f In fldr.SubFolders
GetFolders = GetFolders & GetFolders(f.Path)
DoEvents
Next f
End If
End Function
Then why bother with FSO??? Unless you need an extra dependencies in your project, otherwise use built in Dir function. MSDN has pretty good example how to use it. All you will need (generally speaking) is to check if current file is a "folder". If yes then load its name into your Listview control. Plain and simple and what's more important - NO DEPENDENCIES what-so-ever.
You will need to recurse similar to your example if you are loading a 'treeview' control with the folders. You might want to do this if you are implementing something that looks like a file explorer and you are pairing this with the listview to show the files. But if you are only doing a listview then I usually store the file type in the .tag property when loading. Then when an item is double clicked I look at the type and if it is a folder then I clear the collection and load the new folder. You can store the current folder path in the .tag of the listview itself and then append to it when you load a sub-folder.
Likewise, the loadFolder procedure should add a '..' if the folder has a parent with a special tag to indicate a double click should go up. And going up should remove the last folder from the listview's .tag property.
Hope this helps!
If it fits your needs, the simple VB controls for DriveListBox, DirectoryListBox and FileListBox are the simplest.
They might not always fit your requirements, however, in which case programming your own using a ListBox and the DIR() function will do most things.