chkwah
asked on
source code for seaching in vb5
Can anyone give me a hand to write a seaching program in visual basic?
Searching for files? In multiple folders? I have written some code that searches and returns all files in a folder and it's sub folders, interested?
Marktalbot.. and then a Pascal program to read the file the dir batch file returns.
use FileFindFirst() and FileFindNext()
If you need more HANDm, hehe ;)), lemme know...
..-=ViKtOr=-..
If you need more HANDm, hehe ;)), lemme know...
..-=ViKtOr=-..
ASKER
I need an example of the source code
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's code to list all files in a directory including subdirectories (if you want):
This function will return True if the listing was successful, false otherwhise.
Paramenters in:
Path = the directory you want to search
av_Array() = the array you want to fill with all the filenames
Recursive = boolean value that is True if you want to search all subdirectories, false otherwhise
x = Don't do anything with this, it's only used in recursive calling
StartDir = Don't do anything with this either, it's only used in recursive calling
IncludePaths = if you only want to get the filenames you set this to False but default value is True (you will get full pathnames)
Function ListFiles(ByVal Path As String, ByRef av_Array() As String, Recursive As Boolean, Optional ByRef x As Double = 0, Optional StartDir As Boolean = True, Optional IncludePaths As Boolean = True)
Dim MyName As String
Dim MyDir() As String
Dim MyDirNr As Integer
Dim a As Integer
MyDirNr = 0
ReDim MyDir(0)
If (x = 0) Then
ReDim av_Array(0)
Path = sAddDirSep(Path)
End If
On Error GoTo ErrorHandler
MyName = Dir$(Path + "*.*", vbDirectory + vbArchive + vbHidden + vbReadOnly + vbSystem)
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Path & MyName) And vbDirectory) <> vbDirectory Then
If (x Mod 10 = 0) Then
ReDim Preserve av_Array(x + 10)
End If
x = x + 1
If IncludePaths Then
av_Array(x) = Path + MyName
Else
av_Array(x) = MyName
End If
Else
ReDim Preserve MyDir(UBound(MyDir) + 1)
MyDirNr = MyDirNr + 1
MyDir(MyDirNr) = MyName
End If
End If
MyName = Dir
Loop
If Recursive Then
For a = 1 To MyDirNr
If Not ListFiles(Path + MyDir(a) + "\", av_Array(), True, x, False, IncludePaths) Then GoTo ErrorHandler
Next
End If
If StartDir Then ReDim Preserve av_Array(x)
ListFiles = True
Exit Function
ErrorHandler:
ListFiles = False
End Function
This function will return True if the listing was successful, false otherwhise.
Paramenters in:
Path = the directory you want to search
av_Array() = the array you want to fill with all the filenames
Recursive = boolean value that is True if you want to search all subdirectories, false otherwhise
x = Don't do anything with this, it's only used in recursive calling
StartDir = Don't do anything with this either, it's only used in recursive calling
IncludePaths = if you only want to get the filenames you set this to False but default value is True (you will get full pathnames)
Function ListFiles(ByVal Path As String, ByRef av_Array() As String, Recursive As Boolean, Optional ByRef x As Double = 0, Optional StartDir As Boolean = True, Optional IncludePaths As Boolean = True)
Dim MyName As String
Dim MyDir() As String
Dim MyDirNr As Integer
Dim a As Integer
MyDirNr = 0
ReDim MyDir(0)
If (x = 0) Then
ReDim av_Array(0)
Path = sAddDirSep(Path)
End If
On Error GoTo ErrorHandler
MyName = Dir$(Path + "*.*", vbDirectory + vbArchive + vbHidden + vbReadOnly + vbSystem)
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Path & MyName) And vbDirectory) <> vbDirectory Then
If (x Mod 10 = 0) Then
ReDim Preserve av_Array(x + 10)
End If
x = x + 1
If IncludePaths Then
av_Array(x) = Path + MyName
Else
av_Array(x) = MyName
End If
Else
ReDim Preserve MyDir(UBound(MyDir) + 1)
MyDirNr = MyDirNr + 1
MyDir(MyDirNr) = MyName
End If
End If
MyName = Dir
Loop
If Recursive Then
For a = 1 To MyDirNr
If Not ListFiles(Path + MyDir(a) + "\", av_Array(), True, x, False, IncludePaths) Then GoTo ErrorHandler
Next
End If
If StartDir Then ReDim Preserve av_Array(x)
ListFiles = True
Exit Function
ErrorHandler:
ListFiles = False
End Function
Just to mention.... in order to change the folder searching example to file searching, you simply have to check for faDirectory which is a constant, and if it is then you simypl recursivly call that function on the directory you've found... as simple as that. :)
Hmm i forgot the sAddDirSep function it's supposed to look something like this
Function sAddDirSep(Text as String) As String
sAddDirSep = IIF(Right$(Text, 1) = "\", Text, Text & "\")
End Function
Function sAddDirSep(Text as String) As String
sAddDirSep = IIF(Right$(Text, 1) = "\", Text, Text & "\")
End Function
Then open that file and read the information line by line using line input util you find the information you want.