Results 1 to 2 of 2

Thread: File Search [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127

    File Search [Resolved]

    Looking for a better way to search for a file to see if it exists.

    I am currently using this and it works if you put in the exact directory path. I was wondering if there would be a way to get it to search the entire c: hard drive to see if the file exists even if it's in a sub directory of a subdirectory of a directory etc...

    Code:
    Dim Check As Integer, filepath As String
            filepath = "C:\abc.txt"
            Check = Len(Dir$(filepath))
            If Check = 0 Then
                MessageBox.Show("Found it")
            ElseIf Check <> 0 Then
                MessageBox.Show("Nowhere around")
            End If
    Last edited by teamdad; Aug 3rd, 2004 at 01:08 PM.

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    This code will return the first file it finds.

    Call it like this : MsgBox(Me.Search("D:\","WhatEver.jpg"))

    VB Code:
    1. Private Function Search(ByVal Dir As String, ByVal Find As String) As String
    2.         Dim F As IO.FileInfo = New IO.FileInfo(IO.Path.Combine(Dir, Find))
    3.         If F.Exists = True Then
    4.             Return F.FullName
    5.         End If
    6.  
    7.         Dim sFolders() As String = IO.Directory.GetDirectories(Dir)
    8.         Dim S As String =""
    9.         Dim Q As String = ""
    10.  
    11.         For Each S In sFolders
    12.             Q = Me.Search(S, Find)
    13.             If Q <> "" Then
    14.                 Exit For
    15.             End If
    16.         Next
    17.  
    18.         Return Q
    19.     End Function
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width