PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Len() or fso.FileExists to check if file exists-VBForums
Results 1 to 15 of 15

Thread: [RESOLVED] Len() or fso.FileExists to check if file exists

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    75

    Resolved [RESOLVED] Len() or fso.FileExists to check if file exists

    When working with files, first I check to make sure the file actually exists or it will cause an error. I first learned using File System Object to do this:
    VB Code:
    1. Dim fso As New FileSystemObject
    2. If fso.FileExists App.Path & "\Test.txt" = False Then
    3. MsgBox "The file doesn't exist"
    4. End If
    I then learned about using Len, which apparently saves memory (which makes sense because with File System Object (fso), you need "Microsoft Scripting Runtime 2.0" reference):
    VB Code:
    1. If Len(App.Path & "\Test.txt") = False Then
    2. MsgBox "The file doesn't exist"
    3. End If
    Sometimes, fso works and sometimes Len works. In one project I am working on, Len works better, then later, fso works better, then fso won't work, and now Len won't work again. Is there something I'm missing about these methods that cause them to not work? Or is there some reason that somebody knows that causes them to not work? Thanks.

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Len() or fso.FileExists to check if file exists

    This is what I use.

    VB Code:
    1. If Dir$(App.Path & "\Test.txt") = "" Then
    2. Msgbox "File doesn't exist"
    3. Else
    4. Msgbox "File exists"
    5. End If

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    75

    Re: Len() or fso.FileExists to check if file exists

    Alright, I'll see how that works...oh, somehow I'm getting the error before the checking :O I just started using breakpoints with VB6, they're sure helpful . Looks like I'll be debugging the rest of my program...thanks for the third method

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    75

    Re: Len() or fso.FileExists to check if file exists

    Right, I guess I do have a way that did work, so I could test your method and it did work. Thanks again for the third method. Now I've got to stare at my code that is strangely not working But that's not important here...

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Len() or fso.FileExists to check if file exists

    I don't know of any reason for those methods to just "fail", it must be the way you're using them.

    If your project is already using the FSO, then you can use that method if you like. But I would definitely not reference the FSO just to use it for its FileExists function.

    The Dir$() and FileLen() methods are both good ways of doing it.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    75

    Re: Len() or fso.FileExists to check if file exists

    Quote Originally Posted by DigiRev
    If your project is already using the FSO, then you can use that method if you like. But I would definitely not reference the FSO just to use it for its FileExists function.
    Which is why I don't use it that much any more

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Len() or fso.FileExists to check if file exists

    Doing the file exist check the fastest way without external libraries would be:
    VB Code:
    1. ' string comparisons are slow: take the return string length instead!
    2. If LenB(Dir$("c:\path\to\file.txt")) = 0 Then ' doesn't exist

    FileLen does not work, because a file can exist even if it doesn't contain anything.


    (And I know it doesn't really matter here to have it faster, but keeping this kind of small and easy optimizations in mind can make a difference later on with a looping code.)
    Last edited by Merri; Nov 29th, 2006 at 08:23 PM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    75

    Re: Len() or fso.FileExists to check if file exists

    DigiRev was talking about FileLen...I was just using Len. You're actually right about that. There's a fourth method. Wow, there are a lot of ways to do this

  9. #9
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,607

    Re: [RESOLVED] Len() or fso.FileExists to check if file exists

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Corsair H100i v2 water cooler, Geforce GTX1060, Samsung M.2 500 GB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2010, VS 2010

  10. #10
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: [RESOLVED] Len() or fso.FileExists to check if file exists

    Nice. I'm going to use that from now on.

  11. #11
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,607

    Re: [RESOLVED] Len() or fso.FileExists to check if file exists

    Thanks, just anothe way to skin a cat.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Corsair H100i v2 water cooler, Geforce GTX1060, Samsung M.2 500 GB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2010, VS 2010

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    75

    Re: [RESOLVED] Len() or fso.FileExists to check if file exists

    Wow, another method Seems like a pretty good method, too; Maybe I'll try using that one from now on, too.

  13. #13
    New Member
    Join Date
    Mar 2007
    Posts
    15

    Re: [RESOLVED] Len() or fso.FileExists to check if file exists

    Anyone know how to check without declaring anything?

    THX

  14. #14
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Len() or fso.FileExists to check if file exists

    See post #7 in this thread.

  15. #15
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,129

    Re: [RESOLVED] Len() or fso.FileExists to check if file exists

    Here is the way I check. It doesn't care if the files are hidden or not.
    Code:
    Private Sub Command1_Click()
        MsgBox DoesFileExist("c:\IO.SYS")
    End Sub
    
    Private Function DoesFileExist(ByVal sPath As String) As Boolean
        
        On Error GoTo DoesFileExist_error
        
        DoesFileExist = GetAttr(sPath)
    
    Exit Function
    DoesFileExist_error:
    End Function

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width