opening file in directory

lilgirl

Newcomer
Joined
Jun 21, 2003
Messages
24
I know the name of the file I want to open and I know the root directory it is located in. It is simple to open the file if i know which subdirectory it is in, but my question is: how do I search the root directory and any subdirectories for the file and then open it?
 
You need to implement a FOR Loop that checks all direcotries


and use this method

Visual Basic:
FILE.Exists(FileName)

this has a boolean value if files exists then stop search other wise go to next directory
 
Try this. I have not debug it yet. Basically, it just index through all your subdirectories until find the file that ended with your extention. You might have to fix up some syntax error as I don't have a compiler to check the code at this time :-).

===================================
Private Function FindFile(ByVal tDir As String, ByVal tFile as String) as String
Try
Dim strResult as string
Dim fInfo As FileInfo
Dim dInfo As New DirectoryInfo(tDir)
Dim subDir() As DirectoryInfo
Dim tCount As Integer

For Each fInfo In dInfo.GetFiles
if fInfo.FullName.EndWith(tFile)
Return dInfo.FullName
Next

subDir = dInfo.GetDirectories()

For tCount = 0 To subDir.Length - 1
strResult = FindFile(subDir(tCount).FullName)
If strResult <> Nothing
return strResult
Next

Catch Exp As Exception
MsgBox("Exception: " & Exp.Message)
End Try

return strResult


End Sub
========================================
 
Last edited:
Back
Top