Results 1 to 14 of 14

Thread: [RESOLVED] Check if file exists problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    93

    Resolved [RESOLVED] Check if file exists problem

    Hi

    I have the following code in a module:
    Code:
    Option Explicit
    Private Const OF_EXIST         As Long = &H4000
    Private Const OFS_MAXPATHNAME  As Long = 128
    Private Const HFILE_ERROR      As Long = -1
    
    Private Type OFSTRUCT
        cBytes As Byte
        fFixedDisk As Byte
        nErrCode As Integer
        Reserved1 As Integer
        Reserved2 As Integer
        szPathName(OFS_MAXPATHNAME) As Byte
    End Type
    
    Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
    
    Public Function FileExists(ByVal Fname As String) As Boolean
        Dim lRetVal As Long
        Dim OfSt As OFSTRUCT
    
        lRetVal = OpenFile(Fname, OfSt, OF_EXIST)
        If lRetVal <> HFILE_ERROR Then
            FileExists = True
        Else
            FileExists = False
        End If
    End Function
    If i have a text file in C:
    "C:\111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111.txt"

    Then running FileExists("C:\1111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111.txt") will return true.

    If i add another "1" to that filename i.e make it become:
    "C:\111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 111111111111111111111111.txt"

    Then FileExists("C:\1111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111.txt") returns false although the file exists.

    As long as the length of the filename is less or equal 126 the function works fine. If it's greater than 126 it will always return false

    Any one has a solution to this?

    Thanks,
    Krishley

  2. #2
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    Re: Check if file exists problem

    I always use this function:
    https://stefan.co/basic/visual-basic...on=file_exists

    It works with the files, just checked it out
    If a thread is solved, please click on Thread Tools / Mark Thread Resolved .
    If someone helped you very good, consider rating his post by clicking the icon under his name.

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Check if file exists problem

    This might explain why you are experiencing problems with the file name.

    Edit:

    @Garrcomm

    The original poster is not asking for working code he already said his code works up to a certain point.
    Last edited by Nightwalker83; Jan 25th, 2011 at 05:18 AM. Reason: Adding more!
    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

  4. #4
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Check if file exists problem

    OFS_MAXPATHNAME is 128 bytes. This limits the length of the path when using that API.

    I would suggest using VB's built-in DIR() function. If it returns a non-null string the file exists.

    Or look for a newer version of that API.

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Check if file exists problem

    From the API Documentation
    The OFSTRUCT structure contains a path string member with a length that is limited to OFS_MAXPATHNAME characters, which is 128 characters. Because of this, you cannot use the OpenFile function to open a file with a path length that exceeds 128 characters. The CreateFile function does not have this path length limitation.
    Regarding a solution, use the CreateFile API instead (http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx)

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

    Re: Check if file exists problem

    Honestly, I don't think I'd use OpenFile or CreateFile to check if a file exists. Either may return false depending on permissions of the user. If you don't want to use VB's Dir() function, suggest maybe using GetFileAttributes API or VB's GetAttr function instead.

    Edited: Sample usage from an existing routine of mine
    Code:
    Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    Private Declare Function GetFileAttributesW Lib "kernel32.dll" (ByVal lpFileName As Long) As Long
    Private Const INVALID_HANDLE_VALUE = -1&
    
    ' Unicode API declaration?
    FileExists = Not (GetFileAttributesW(StrPtr(FileName)) = INVALID_HANDLE_VALUE)
    
    ' ANSI API declaration?
    FileExists = Not (GetFileAttributes(FileName) = INVALID_HANDLE_VALUE)
    Last edited by LaVolpe; Jan 25th, 2011 at 09:29 AM. Reason: oops, didn't include the ANSI declaration. Done
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Check if file exists problem

    Here's the API I use. Short and sweet, plus it allows you to differentiate between files and folders.
    vb Code:
    1. Private Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
    2. Private Declare Function PathIsDirectory Lib "shlwapi" Alias "PathIsDirectoryA" (ByVal pszPath As String) As Long
    3.  
    4. Public Function FileExists(pstrFile As String) As Boolean
    5.     FileExists = (PathFileExists(pstrFile) = 1)
    6.     If FileExists Then FileExists = (PathIsDirectory(pstrFile) = 0)
    7. End Function
    8.  
    9. Public Function FolderExists(pstrFolder As String) As Boolean
    10.     FolderExists = (PathFileExists(pstrFolder) = 1)
    11.     If FolderExists Then FolderExists = (PathIsDirectory(pstrFolder) <> 0)
    12. End Function

  8. #8
    Lively Member Garrcomm's Avatar
    Join Date
    Jul 2009
    Location
    the Netherlands
    Posts
    87

    Red face Re: Check if file exists problem

    Quote Originally Posted by Nightwalker83 View Post
    @Garrcomm

    The original poster is not asking for working code he already said his code works up to a certain point.
    True, but replacing the function for a working one would help as well I suppose

    Didn't knew the Dir()-function though, updated my own code, nice one
    If a thread is solved, please click on Thread Tools / Mark Thread Resolved .
    If someone helped you very good, consider rating his post by clicking the icon under his name.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    93

    Re: Check if file exists problem

    Hi,
    Thanks everyone for the help. The links were really useful. I ended up using the Dir() function.

    @Garrcomm: All suggestions are always welcomed

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

    Re: [RESOLVED] Check if file exists problem

    Be forewarned. Dir() can return incorrectly if the file attributes are not used.

    On a hidden file: Dir([the hidden file]) = "" even though the file eixsts
    But Dir([the hidden file], vbHidden) = [the hidden file]

    Suggestion, use all of them:
    If Dir([whatever], vbHidden Or vbReadOnly Or vbSystem Or vbArchive)<>"" Then...
    And for folders, include the vbDirectory flag also
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: [RESOLVED] Check if file exists problem

    One note about Dir and using vbDirectory on sub-directories is the first two results returned become "." and "..". Where "." means current directory, and ".." is the parent directory.

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

    Re: [RESOLVED] Check if file exists problem

    Quote Originally Posted by FireXtol View Post
    One note about Dir and using vbDirectory on sub-directories is the first two results returned become "." and "..". Where "." means current directory, and ".." is the parent directory.
    FireXtol, depends on trailing slash.

    Dir("C:\Program Files\", vbDirectory) = "."
    Dir("C:\Program Files", vbDirectory) = "Program Files"
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    93

    Re: [RESOLVED] Check if file exists problem

    Thanks LaVolpe. I had already compiled my s/w without adding those flags. I've just added them. Thanks once again to everyone!

    Regards,
    Krishley
    My Software : Kestrel GX

  14. #14
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] Check if file exists problem

    Keep in mind that Dir() is global, meaning (for example) you can't be in the middle of a Dir() loop and then use your function to see if a similarly-named file is on another drive. Well, you can, but doing so will short-circuit the Dir() loop.

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