Results 1 to 6 of 6

Thread: File Exists

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Posts
    55
    i need a function that returns true or false, wether a file exists or not

  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    Thumbs up

    Here you go... Add a textbox and a commandbutton to a form to test this example:

    Code:
    Private Sub Command1_Click()
    If Dir(Text1.Text) <> "" Then
        MsgBox "File """ & Text1.Text & """ found!"
    Else
        MsgBox "File """ & Text1.Text & """ not found!"
    End If
    End Sub
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  3. #3
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175
    You can use the following code as a function

    Function File_Exist(FileName As String) As Integer
    Dim Cp As Integer
    On Local Error Resume Next
    Cp = FreeFile
    Open FileName For Input As Cp
    File_Exist = IIf(Err, True, False)
    Close Cp
    Err = 0
    End Function
    Mass

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb

    Juz use Dir function is enough.

  5. #5
    Guest
    Code:
    Public Function fileexists(strfilepathandname As String) As Boolean
        On Error Resume Next
        Dim lngcheck As Long
        If InStr(strfilepathandname$, ".") = 0& Then
            Let fileexists = False
        End If
        Let lngcheck& = Len(Dir$(strfilepathandname$))
        If lngcheck& = 0& Then
            Let fileexists = False
            Exit Function
        Else
            Let fileexists = True
        End If
        Exit Function
    End Function
    
    'Usage:
    'If fileexists("C:\Autoexec.bat") Then
    'Msgbox "file exists."
    'Else
    'Msgbox "file does not exist."
    'End If

  6. #6
    Hyperactive Member
    Join Date
    Dec 1999
    Posts
    321

    Lightbulb Use FSO


    Code:
    'Declaration
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Syntax: object.fileexists (filespec)
    
    'Example:
    If fso.FileExists("C:\Windows\win.com") Then
        'Code if exists
    Else
        'Code if not
    End If

    Signed, Rodik ([email protected])
    Programmer,usesVB6ED
    ===========================
    Copyright©RodikCo,2002.

    Dont mind this signature ;] Its old

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