Results 1 to 5 of 5

Thread: search for file in a directory

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    6
    Please tell me a good way to search a file in the hard drive given i know the file path. i want to search a file in a dirctory. for example, lets assume i have directory call "apps" and i want to search for file in this dirctory.

  2. #2
    Guest
    Use the Dir function.

    Code:
    If Dir("C:\apps\myfile.exe") <> "" Then
    Msgbox "file exists"
    Else
    Msgbox "file not found"
    End If

  3. #3
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    'Put in form
    If FileExists("c:\myapps\myfile.exe") Then
    'file exists
    else
    'file does NOT exists
    End if


    'Put in module
    Public Function FileExists(strPath) As Boolean

    Dim fnum
    fnum = FreeFile
    FileExists = True
    On Error GoTo err
    Open strPath For Input As fnum
    Close fnum
    Exit Function
    err:
    Close fnum
    FileExists = False
    End Function

  4. #4
    Guest
    Dir function is much simpler and faster as well (I believe) .

    If you want it in a function:

    Code:
    Private Function FileExists(file As String) As Boolean
    If Dir(file) <> "" Then
    FileExists = True
    Else
    FileExists = False
    End If
    End Function
    
    
    Usage:
    
    If FileExists("C:\apps\myfile.exe") Then
    Msgbox "file exists"
    Else
    Msgbox "file not found"
    End If

  5. #5
    Lively Member
    Join Date
    Aug 2000
    Location
    UK
    Posts
    78
    Yes, I used to use the DIR, but becomes useless when you're checking the existence of a file whilst already in a loop using DIR.

    Solved many debug problems by dropping the DIR in favour of other methods.


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