LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Dir function to check for file not reliable

Just discovered that you can't rely on the old Dir function to check if a
file exists or not and I thought it might be worth it to post this to this
forum.

I always used a function like this to test if a file exists:

Function bFileExists3(ByVal sFile As String) As Boolean

bFileExists3 = Len(Dir(sFile)) 0

End Function

This is just no good, try:

MsgBox bFileExists3(""), , Dir("")

I get True as Dir("") gives me: 256 colours.htm


When I make the function like this:

Function bFileExists3(ByVal sFile As String) As Boolean

bFileExists3 = Len(Dir(sFile)) 0 And Len(sFile) 0

End Function

It will work in VBA, but not when I make an ActiveX dll in VB6.
Strangely, the len function doesn't give zero.
I have no public or private variables that could mess the function up.

I have found 2 alternatives to check if a file exists or not, the first one
is from
Randy Birch's site. Both seem to work fine.

Option Explicit
Private Declare Function PathFileExists Lib "shlwapi" _
Alias "PathFileExistsA" _
(ByVal pszPath As String) As Long

Function bFileExists(ByVal sPath As String) As Boolean

'Determines if a file exists. This function
'tests the validity of the file and path. It
'works only on the local file system or on a
'remote drive that has been mounted to a drive
'letter.
'
'It will return False for remote file paths
'that begin with the UNC names \\server
'or \\server\share. It will also return False
'if a mounted remote drive is out of service.
'
'Requires Version 4.71 and later of Shlwapi.dll

bFileExists = PathFileExists(sPath) = 1

End Function

Function bFileExists2(ByVal sFile As String) As Boolean

Dim lAttr As Long

On Error Resume Next
lAttr = GetAttr(sFile)
bFileExists2 = (Err.Number = 0) And ((lAttr And vbDirectory) = 0)
On Error GoTo 0

End Function


Will stick with the API one for now.


RBS


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is Excel reliable ראובן Excel Discussion (Misc queries) 18 August 8th 05 02:26 PM
Check who's using a file Luis Excel Programming 1 August 6th 04 03:31 PM
Excel 97 VBA - CURDIR, not reliable Ricke G Excel Programming 2 July 28th 04 07:52 PM
FAST, RELIABLE, LEGAL MONEY MAKING OPPORTUNITY Vasant Nanavati Excel Programming 1 February 19th 04 03:42 AM
excel 2000 workbook.activate not 100% reliable with alt+tab Gavin Frayne Excel Programming 9 July 24th 03 09:29 AM


All times are GMT +1. The time now is 07:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright 2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"