Computing.Net > Forums > Programming > Visual Basic 6.0 - URGENT HELP!!!!!

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Visual Basic 6.0 - URGENT HELP!!!!!

Reply to Message Icon

Original Message
Name: NeedHelpPlease
Date: January 14, 2003 at 10:01:52 Pacific
Subject: Visual Basic 6.0 - URGENT HELP!!!!!
OS: Windows XP Pro
CPU/Ram: 2.8GHz P4 'B', 512MB DDR
Comment:

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]



Report Offensive Message For Removal


Response Number 1
Name: tech-fred
Date: January 14, 2003 at 14:48:32 Pacific
Reply: (edit)

I don't know if this works on an XP. Works in
Win98SE. Rum DOS Command "DIR /S /B > File"
It will give a full list of flles and sub-
directories below the startng directory.


Report Offensive Follow Up For Removal

Response Number 2
Name: rap-rap
Date: January 17, 2003 at 23:34:02 Pacific
Reply: (edit)

research on drive list box, file list box.. look for it in your components tab... create an invisible list box, set the properties of the listbox in code and then jjust try to print the contents of those.. email if not done..I'll do it for u. i'm in a hurry just read ur question.. :-)



Report Offensive Follow Up For Removal

Response Number 3
Name: Trippy
Date: January 18, 2003 at 10:16:45 Pacific
Reply: (edit)

Don't know about the sub DIR's, but to get all files in current directory, click on the file list box (If you put one on the form.) and in the "Properties" box, there should be an option that says "Pattern." Make sure it says *.* :)

-Matt


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software