Results 1 to 15 of 15

Thread: Re: Classic VB - How can I check if a file exists?

Hybrid View

  1. #1
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,654

    Re: Classic VB - How can I check if a file exists?

    IMO, the best and easiest way is to simply use the API function.

    Code:
    Public Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
    
    If PathFileExists("C:\whatever.wtv") Then
    It returns 0 if the file/folder doesn't exist. There's also PathFileExistsW if you're dealing with Unicode. I use this function extensively, it works fine in VB. Just pass the full directory info.
    Last edited by fafalone; Jul 6th, 2013 at 06:57 PM.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Classic VB - How can I check if a file exists?

    Quote Originally Posted by fafalone View Post
    IMO, the best and easiest way is to simply use the API function.

    Code:
    Public Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
    
    If PathFileExists("C:\whatever.wtv") Then
    It returns 0 if the file/folder doesn't exist. There's also PathFileExistsW if you're dealing with Unicode. I use this function extensively, it works fine in VB. Just pass the full directory info.
    From Visual C++ in Short: Determining whether a path refers to a file system object:

    Quote Originally Posted by Kenny Kerr
    The shell provides the PathFileExists function which is simpler than the approaches mentioned thus far but is limited in that it does not distinguish between files and directories.

    . . .

    Incidentally, the PathFileExists function I mentioned above uses GetFileAttributes internally if it determines that you’re running on a supported version of Windows.
    From Superstition: Why is GetFileAttributes the way old-timers test file existence?:

    Quote Originally Posted by Raymond Chen
    If you ask an old-timer how to test for file existence, they'll say, "Use GetFileAttributes." This is still probably the quickest way to test for file existence, since it requires only a single call. Other methods such as FindFirstFile or CreateFile require a separate FindClose or CloseHandle call, which triggers another network round-trip, which adds to the cost.
    Through extensive research and experimentation, I believe I've found The Optimum FileExists Function.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

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