Ok, I've got a problem with my project and its not a very big one. I know this will be really easy for someone brainy to figure out.
All I want is: to get a list of ALL the files from a given path.
For example lets say c:\windows\*.*. I also want all files in the sub-dirs of that given path (c:\windows\system\*.*, c:\windows\temp\*.*, etc) and the sub-dir's of the sub-dir (c:\windows\temp\misc\*.*, etc) and so on until all files have been retreived from all levels of directories from the original given path. I want the [i]full path[/i] of each file to be wacked straight in to an array, listbox or whatever. It doesn't matter.
I'm desperate for working code, I've searched everywhere from planet sourcecode to well... every other place. and now I'm at my wits end and on my 6th cup of coffee trying my best to figure out what I'm doing wrong :eek:
Here's the code I've tried to write myself, it doesn't work properly though. It only gets files from the given path and the FIRST level of subdir and thats it. Please help me fix this someone!
p.s If you think this code is crap and is long winded, your probably right because I think it is as well. Please show me a better way!!
p.p.s This code is part of a module. All relevant code is included.
[QUOTE]
Public Function GetPath(FilePath As String) As String
Dim counter As Integer
For counter = Len(FilePath) To 1 Step -1
If Mid(FilePath, counter, 1) = "\" Then
GetPath = Mid(FilePath, 1, counter)
Exit Function
End If
Next counter
End Function
Public Sub Gather(GatherPath As String, GatherFileList As ListBox, GatherDirList As ListBox, GatherSubDirList As ListBox)
'This sub will gather all files from a given path
'Check and fix current path
If Right(GatherPath, 1) "\" Then
GatherPath = GatherPath + "\"
End If
'Get all dirs in given path root
GatherDir = Dir(GatherPath, vbDirectory)
Do While GatherDir ""
'Use bitwise comparison to make sure we got a dir.
If (GetAttr(GatherPath & GatherDir) And vbDirectory) = vbDirectory And Right(GatherPath & GatherDir, 1) "." Then
'List it up only if its defintely a dir
GatherDirList.AddItem (GatherPath & GatherDir)
End If
GatherDir = Dir() 'Get next dir
DoEvents 'Give OS a breather
Loop
GetDirs:
'Get all subdirs (if any) in their parent dir(s)
If GatherDirList.ListCount 0 Then 'Have we got any root dirs to work with?
For counter = 0 To GatherDirList.ListCount - 1
GatherDir = Dir(GatherDirList.List(counter), vbDirectory)
Do While GatherDir ""
'Use bitwise comparison to make sure we got a dir.
If (GetAttr(GatherPath & GatherDir) And vbDirectory) = vbDirectory And Right(GatherPath & GatherDir, 1) "." Then
'List it up only if its defintely a dir and not itself or a parent (i.e "\.." or "\.")
GatherSubDirList.AddItem (GatherPath & GatherDir)
End If
GatherDir = Dir() 'Get next dir
DoEvents 'Give OS a breather
Loop
Next
End If
'Add the subdirs to the main dir list
If GatherSubDirList.ListCount 0 Then 'Have we got any sub dirs to work with?
'Remember how many sub dirs we had
ListCount = GatherSubDirList.ListCount
'Reset varibles
flag = 0
RemovedCount = 0
'Check for duplicate entries of subdirs in the main list
For counter = 0 To GatherSubDirList.ListCount - 1
For counter2 = 0 To GatherDirList.ListCount - 1
If GatherDirList.List(counter2) = GatherSubDirList.List(counter) Then
GatherSubDirList.RemoveItem (counter) 'We've got it already, get rid of it
RemovedCount = RemovedCount + 1
End If
Next
Next
If GatherSubDirList.ListCount 0 Then 'Have we got any sub dirs left to work with?
'Add them to the main dir list
For counter = 0 To GatherSubDirList.ListCount - 1
GatherDirList.AddItem GatherSubDirList.List(counter)
GatherSubDirList.RemoveItem (counter)
Next
End If
If ListCount RemovedCount Then
'Re-run process until we've found everything
GoTo GetDirs
End If
End If
'Get all the files in current path
GetFiles:
GatherFile = Dir(GatherPath & "*.*", vbHidden)
Do While GatherFile ""
GatherFileList.AddItem (GatherPath & GatherFile)
GatherFile = Dir()
DoEvents 'Give OS a breather
Loop
If GatherDirList.ListCount 0 Then 'Have we got any dirs to work with?
GatherPath = GatherDirList.List(GatherDirList.ListCount - 1) & "\" 'change scope to new path
GatherDirList.RemoveItem (GatherDirList.ListCount - 1)
GoTo GetFiles
End If
frmMain.Status.Text = "[" & GatherFileList.ListCount & " files found]"
End Sub
[/QUOTE]