Results 1 to 26 of 26

Thread: [RESOLVED] File or Directory Exists

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Resolved [RESOLVED] File or Directory Exists

    What is the best function to tell if a file or directory exists? I'm trying to use the Dir function but I don't know what attributes to use. I want the function to be very versatile so that it can tell if a file or directory exists no matter what attributes it has. Thank you

  2. #2
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    Re: File or Directory Exists

    You can do it this way:

    Code:
    Dim FilePath as string
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    FilePath = 'set your full path and filename here'
    If fs.FileExists(FilePath) Then
          Msgbox  "The file does exist"
    Else
          Msgbox "File does not exist"
    endIf

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    Does anyone know if there's certain times when Dir doesn't work right? Can you please give an example? Thanks

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: File or Directory Exists

    As far as I know it works well in all cases as long as the appropriate attributes are set. It does not work well for unicode file paths or unicode file names. Regarding attributes -- can fail to return hidden items if vbHidden attr not set. The following should always return anything in the target folder:

    File:
    Dir$("somePath\*.*", vbHidden Or vbReadOnly Or vbSystem or vbArchive)
    Directory:
    Dir$("somePath\", vbHidden Or vbReadOnly Or vbSystem or vbArchive or vbDirectory)


    Maybe others may chime in with specific cases where it could fail even with the above command.
    Last edited by LaVolpe; Dec 19th, 2007 at 09:49 PM.

  5. #5
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: File or Directory Exists

    Quote Originally Posted by abazabam
    What is the best function to tell if a file or directory exists? I'm trying to use the Dir function but I don't know what attributes to use. I want the function to be very versatile so that it can tell if a file or directory exists no matter what attributes it has. Thank you
    Use the attributes vbSystem + vbHidden. That will return any type of file or directory.

  6. #6
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: File or Directory Exists

    Quote Originally Posted by LaVolpe
    [Snip]
    Dir$("somePath\*.*", vbHidden Or vbReadOnly Or vbSystem or vbArchive)

    Maybe others may chime in with specific cases where it could fail even with the above command.
    I don't think you need the vbReadOnly or the vbArchive flags -- as far as I can tell they don't have any effect on the Dir$ function. I could be wrong though.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: File or Directory Exists

    You may very well be correct; probably old habits talking, on my part.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    Also, does anyone know how to read or get the file size of a file with bad characters in it? The bad characters are replaced with a question mark in VB.

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

    Re: File or Directory Exists

    Thats not bad characters, its a unicode character.

    Also, another way to check a file exists - http://vbforums.com/showthread.php?t=349990
    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, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    Well, how can I check its file size? When I try, it gives me an error that says something like "Bad filename or number"

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

    Re: File or Directory Exists

    What code are you using to check filesize?
    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, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    448

    Arrow Re: File or Directory Exists

    Why not just use FileLen(yourfile) to get its size... also another way to check existance of a file:

    Function FileExists(FileName As String) As Boolean
    On Error Resume Next
    ' get the attributes and ensure that it isn't a directory
    FileExists = (GetAttr(FileName) And vbDirectory) = 0
    End Function


    If FileExists(YourFile) then
    msgbox "Exists"
    Else
    msgbox "Does not Exist"
    End If
    "Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    Quote Originally Posted by RobDog888
    What code are you using to check filesize?
    I'm using FileLen. I'm guessing I'm getting the error because the filename with "bad characters" are replaced with question marks in VB and question marks can't be used in filenames.

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: File or Directory Exists

    As I was saying, there are not questionmarks but rather unsupported unicode characters. You are reading the file with the wrong encoding standard or a standard that doesnt support unicode characters.

    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, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: File or Directory Exists

    Another very important question. How are you getting the filename? If using commondialog control, it will not return unicode strings.

    If your app is to support unicode paths/filenames, you will need to move to APIs to display a unicode-friendly open/browse dialog window, and also to open/create files and retrieve file attributes.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    I'm actually searching for files using FindFirstFile and FindNextFile.

  17. #17
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: File or Directory Exists

    Same issue. Those have an ANSI (FindFirstFileA) version & unicode version (FindFirstFileW). The ANSI version won't return unicode text.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    Okay, thanks, I'll try it.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    Should I change all of the APIs' "A" to a "W"?

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: File or Directory Exists

    Change to the "W" so you can enable the unicode support.
    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, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  21. #21
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: File or Directory Exists

    Another way

    Code:
    Private Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
    Private Declare Function PathIsDirectory Lib "shlwapi" Alias "PathIsDirectoryA" (ByVal pszPath As String) As Long
    
    Public Function FolderExists(ByVal pstrFolder As String) As Boolean
        FolderExists = (PathFileExists(pstrFolder) = 1)
        If FolderExists Then FolderExists = (PathIsDirectory(pstrFolder) <> 0)
    End Function
    
    Public Function FileExists(ByVal pstrFile As String) As Boolean
        FileExists = (PathFileExists(pstrFile) = 1)
        If FileExists Then FileExists = Not (PathIsDirectory(pstrFile) <> 0)
    End Function
    IIF(Post.Rate > 0 , , )

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    Quote Originally Posted by RobDog888
    Change to the "W" so you can enable the unicode support.
    Are there any disadvantages?

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: File or Directory Exists

    Possibly but none that I know of.
    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, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    Actually, I can't get it to work anymore since I changed them to "W"s. Was I supposed to do something else like StrConv?

  25. #25
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: File or Directory Exists

    Using unicode apis are not always that easy. Most parameters in the API that are string need to be handled differently; two options (examples):
    1) Pass the string parameter like: StrConv()
    == FindFirstFileW(StrConv(sCriteria, vbUnicode), wfd)
    2) Change API param type from Byval String to ByVal Long and pass the string parameter as StrPtr()
    == FindFirstFileW(StrPtr(sCriteria), wfd)


    Now when you get your FIND DATA structure back, you probably were looking for vbNullChar in the cFileName member, now you will look for vbNullChar & vbNullChar and use StrConv(wfd.cFileName, vbFromUnicode) if the filename is needed.
    Last edited by LaVolpe; Dec 21st, 2007 at 07:06 PM.

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: File or Directory Exists

    Okay, thank you.

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