Results 1 to 10 of 10

Thread: Determine if file exists

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    137

    Determine if file exists

    Hello,

    I would like to have a function or so can check if a given file exists.

    Can anyone help please.......


    Thanks

    vbBoy.

  2. #2
    Lively Member dlm's Avatar
    Join Date
    Oct 2000
    Location
    Geraardsbergen(Belgium)
    Posts
    91
    Hi,

    You can use this code

    Code:
    Public Function FileExists(Bestand As String) As Boolean
        FileExists = (Dir(Bestand) <> "")
    End Function
    
    
    Public Function Run_Update_Quotations()
        strSource = "\\Euermw01\Access\SalesDB\Data\Quotations_Data.mdb"
        strDestination = "\\Euermw01\Access\SalesDB\Data\Quotations_Backup_" & _
                         Day(Now()) & "_" & Month(Now()) & "_" & Year(Now()) & ".mdb"
        If Not FileExists(strDestination) Then
            FileCopy strSource, strDestination
        End If
    
    ...
    End Function
    If you can solve the problem, why worrying about it…
    If you can’t solve the problem, worrying won’t help…

    De la Motte Günther

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    VB Code:
    1. '' Does a given file exist ?
    2. Private Function Exists(ByVal strFullPath As String) As Boolean
    3.     Exists = Len(Dir(strFullPath)) <> 0
    4. End Function
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    use the Dir() funciton.

    usage Dir("filepath")

    example:
    VB Code:
    1. If Dir("C:\bob.txt") Then 'File exists
    2.     'you code here
    3. End if
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Originally posted by darre1
    use the Dir() funciton.

    usage Dir("filepath")

    example:
    VB Code:
    1. If Dir("C:\bob.txt") Then 'File exists
    2.     'you code here
    3. End if
    Sorry to say this but your code doesn't work.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    teeheehee
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    you're quite right.
    should be:

    VB Code:
    1. If Dir("C:\bob.txt") <> "" Then 'File exists
    2.     'you code here
    3. End if
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  8. #8
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Originally posted by plenderj
    teeheehee
    bollocks

    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Thats the normal way of doing it, but my approach is actually faster as far as I know.
    Comparing numbers is faster than strings you see
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    true.
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

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