Given a folder path, what would be the easiest way to loop through and find all filenames and folder names in the given path? Thanks.
Printable View
Given a folder path, what would be the easiest way to loop through and find all filenames and folder names in the given path? Thanks.
Does it have to be in the code? Because you can use a FileListBox and in Form_Load event put File1.Path = "Location of folder"
I have to be able to retreive the filepaths of the folders and files so I can work with them programatically.
This loads the jpg images from a folder into a listbox, but you can change it to suit your wishes.
VB Code:
Option Explicit Private Sub Form_Load() Dim FileFinder$ FileFinder = Dir("d:\temp\*.jpg") Do Until FileFinder = "" List1.AddItem LCase(FileFinder) FileFinder = Dir Loop End Sub
Ok, that'll work great. Now, how would I find all the folders in the specified path?
Try this code, it will walk the directory structure and give you all the folders and files within that directory. This example will fill a treeview with the same tree structure as the directory.
VB Code:
Private Sub ShowFolder(ByVal sFolder As String) Dim sDir As String Dim coll As Collection Dim i As Long Set coll = New Collection sDir = Dir(sFolder & "\", vbDirectory + vbHidden + vbReadOnly) With TreeView1 Do While sDir <> "" If (GetAttr(sFolder & "\" & sDir) And vbDirectory) = vbDirectory Then If sDir <> "." And sDir <> ".." Then .Nodes.Add sFolder, tvwChild, sFolder & "\" & sDir, sDir coll.Add sFolder & "\" & sDir End If Else 'It may be a file .Nodes.Add sFolder, tvwChild, sFolder & "\" & sDir, sDir End If sDir = Dir Loop .Nodes(sFolder).Sorted = True End With For i = 1 To coll.Count ShowFolder coll(i) Next Set coll = Nothing End Sub Private Sub Command1_Click() With TreeView1 .Nodes.Clear .Nodes.Add , , Text1.Text, Dir(Text1.Text, vbDirectory) ShowFolder Text1.Text End With End Sub
hope this helps
File list : http://www.vbforums.com/showthread.php?t=244915
Folder list : http://www.vbforums.com/showthread.php?t=244880
Ok, thanks.