|
-
Jul 6th, 2013, 06:53 PM
#1
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.
-
Jul 7th, 2013, 01:55 AM
#2
Re: Classic VB - How can I check if a file exists?
 Originally Posted by fafalone
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:
 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?:
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|