+ Reply to Thread
Results 1 to 6 of 6

Existing file ?

  1. #1
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    Existing file ?

    Im after some code that will check to see if a file exists in a specific location.

    Ive been given the below two examples but i cant get them to work.


    example 1

    if Not Dir("C:\DATA\YOURFILE.XLS") then
    'the bugger isnot there!
    end if


    example2

    Sub DoesFileExist()
    Set MyFile = CreateObject("Scripting.FileSystemObject")
    MsgBox MyFile.FileExists("c:\filename.xls")
    End Sub

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon Ceemo

    My example works fine for me when called from a macro. Copy it into your VBE and run it. making sure that you don't have a file called "c:\book1.xls" in that location. The result will be false. Change line 3 to a file that does exist and run it again to get true.

    Sub DoesFileExist()
    Set MyFile = CreateObject("Scripting.FileSystemObject")
    MsgBox MyFile.FileExists("c:\book1.xls")
    End Sub

    HTH

    DominicB

  3. #3
    Peter T
    Guest

    Re: Existing file ?

    Hi ceemo,

    This works for me -

    Sub test()
    Dim bGotIt As Boolean
    Dim sFile As String
    Dim sPath As String

    sFile = "Book1.xls"
    sPath = "c:\temp\"

    'On Error Resume Next
    bGotIt = (LCase(sFile) = LCase(Dir(sPath & sFile)))
    'On Error GoTo 0

    MsgBox bGotIt

    End Sub

    I though that would need error handling if false, but doesn't seem
    necessary.

    Regards,
    Peter T

    "ceemo" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Im after some code that will check to see if a file exists in a specific
    > location.
    >
    > Ive been given the below two examples but i cant get them to work.
    >
    >
    > example 1
    >
    > if Not Dir("C:\DATA\YOURFILE.XLS") then
    > 'the bugger isnot there!
    > end if
    >
    >
    > example2
    >
    > Sub DoesFileExist()
    > Set MyFile = CreateObject("Scripting.FileSystemObject")
    > MsgBox MyFile.FileExists("c:\filename.xls")
    > End Sub
    >
    >
    > --
    > ceemo
    > ------------------------------------------------------------------------
    > ceemo's Profile:

    http://www.excelforum.com/member.php...o&userid=10650
    > View this thread: http://www.excelforum.com/showthread...hreadid=400396
    >




  4. #4
    Tom Ogilvy
    Guest

    Re: Existing file ?

    if Dir("C:\DATA\YOURFILE.XLS") ="" then
    'the bugger isnot there!
    end if

    --
    Regards,
    Tom Ogilvy

    "ceemo" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Im after some code that will check to see if a file exists in a specific
    > location.
    >
    > Ive been given the below two examples but i cant get them to work.
    >
    >
    > example 1
    >
    > if Not Dir("C:\DATA\YOURFILE.XLS") then
    > 'the bugger isnot there!
    > end if
    >
    >
    > example2
    >
    > Sub DoesFileExist()
    > Set MyFile = CreateObject("Scripting.FileSystemObject")
    > MsgBox MyFile.FileExists("c:\filename.xls")
    > End Sub
    >
    >
    > --
    > ceemo
    > ------------------------------------------------------------------------
    > ceemo's Profile:

    http://www.excelforum.com/member.php...o&userid=10650
    > View this thread: http://www.excelforum.com/showthread...hreadid=400396
    >




  5. #5
    Forum Contributor
    Join Date
    06-15-2004
    Posts
    246

    What if a:

    This works great but if i am pointing to th a: drive and there isnt a floppy in it the macro will fall over. Is there anyway arond this?




    Sub test()
    Dim bGotIt As Boolean
    Dim sFile As String
    Dim sPath As String

    sFile = "Book1.xls"
    sPath = "c:\temp\"

    'On Error Resume Next
    bGotIt = (LCase(sFile) = LCase(Dir(sPath & sFile)))
    'On Error GoTo 0

    MsgBox bGotIt

    End Sub

  6. #6
    Dave Peterson
    Guest

    Re: Existing file ?

    One way:

    Option Explicit
    Sub test()
    Dim bGotIt As Boolean
    Dim sFile As String
    Dim sPath As String
    Dim testStr As String

    sFile = "Book1.xls"
    sPath = "a:\temp\"

    testStr = ""
    On Error Resume Next
    bGotIt = (Len(testStr) > 0)
    On Error GoTo 0

    MsgBox bGotIt

    End Sub




    ceemo wrote:
    >
    > This works great but if i am pointing to th a: drive and there isnt a
    > floppy in it the macro will fall over. Is there anyway arond this?
    >
    > Sub test()
    > Dim bGotIt As Boolean
    > Dim sFile As String
    > Dim sPath As String
    >
    > sFile = "Book1.xls"
    > sPath = "c:\temp\"
    >
    > 'On Error Resume Next
    > bGotIt = (LCase(sFile) = LCase(Dir(sPath & sFile)))
    > 'On Error GoTo 0
    >
    > MsgBox bGotIt
    >
    > End Sub
    >
    > --
    > ceemo
    > ------------------------------------------------------------------------
    > ceemo's Profile: http://www.excelforum.com/member.php...o&userid=10650
    > View this thread: http://www.excelforum.com/showthread...hreadid=400396


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1