Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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?
Posted

You need to implement a FOR Loop that checks all direcotries

 

 

and use this method

 

FILE.Exists(FileName)

 

this has a boolean value if files exists then stop search other wise go to next directory

The one and only

Dr. Madz

eee-m@il

Posted (edited)

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

========================================

Edited by Hughng

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...