ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Dir function to check for file not reliable (https://www.excelbanter.com/excel-programming/351446-dir-function-check-file-not-reliable.html)

RB Smissaert

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



Patrick Molloy[_2_]

Dir function to check for file not reliable
 
set a reference to Microsoft's Scripting Runtime DLL. Create an object as a
Scripting.FileSystemObject then you can use the .FileExists method to check
er if a file exists ...you can also use th e.FolderExists method & navigate
the tree this way.


"RB Smissaert" wrote:

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




Tom Ogilvy

Dir function to check for file not reliable
 
Seems like you just need to add a check in your function for a valid file
name. To the best of my knowledge, "" isn't a valid filename. Apparently
Dir accepts it as a valid argument to return any file in the directory.

perhaps test

Function bFileExists3(ByVal sFile As String) As Boolean

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

End Function


--
Regards,
Tom Ogilvy


"RB Smissaert" wrote in message
...
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





RB Smissaert

Dir function to check for file not reliable
 
That is what I did, but with the Len function.
It solves it for my code in VBA, but strangely not for my
code in the VB6 dll.
I have gone with this one now:

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

RBS


"Tom Ogilvy" wrote in message
...
Seems like you just need to add a check in your function for a valid file
name. To the best of my knowledge, "" isn't a valid filename. Apparently
Dir accepts it as a valid argument to return any file in the directory.

perhaps test

Function bFileExists3(ByVal sFile As String) As Boolean

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

End Function


--
Regards,
Tom Ogilvy


"RB Smissaert" wrote in message
...
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






RB Smissaert

Dir function to check for file not reliable
 
I try to avoid setting more references as there always is the chance that
something will go wrong with it. Will stick for now with this:

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

RBS

"Patrick Molloy" wrote in message
...
set a reference to Microsoft's Scripting Runtime DLL. Create an object as
a
Scripting.FileSystemObject then you can use the .FileExists method to
check
er if a file exists ...you can also use th e.FolderExists method &
navigate
the tree this way.


"RB Smissaert" wrote:

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





Peter T

Dir function to check for file not reliable
 
Hi Bart,

I just tried in an ActiveX dll and the simple way works fine with both these
additional "checks"

'bFileExists3 = Len(Dir(sFile)) 0 And sFile < "" ' or vbNullString
bFileExists3 = Len(Dir(sFile)) 0 And Len(sFile)

In fact, I don't see how either "check" could fail to return false if sFile
is zero length

Regards,
Peter T

"RB Smissaert" wrote in message
...
That is what I did, but with the Len function.
It solves it for my code in VBA, but strangely not for my
code in the VB6 dll.
I have gone with this one now:

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

RBS


"Tom Ogilvy" wrote in message
...
Seems like you just need to add a check in your function for a valid

file
name. To the best of my knowledge, "" isn't a valid filename.

Apparently
Dir accepts it as a valid argument to return any file in the directory.

perhaps test

Function bFileExists3(ByVal sFile As String) As Boolean

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

End Function


--
Regards,
Tom Ogilvy


"RB Smissaert" wrote in message
...
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








RB Smissaert

Dir function to check for file not reliable
 
Hi Peter,

Yes, I agree, I must have made a mistake somewhere.
If it works in VBA it should work in VB6.

RBS

"Peter T" <peter_t@discussions wrote in message
...
Hi Bart,

I just tried in an ActiveX dll and the simple way works fine with both
these
additional "checks"

'bFileExists3 = Len(Dir(sFile)) 0 And sFile < "" ' or vbNullString
bFileExists3 = Len(Dir(sFile)) 0 And Len(sFile)

In fact, I don't see how either "check" could fail to return false if
sFile
is zero length

Regards,
Peter T

"RB Smissaert" wrote in message
...
That is what I did, but with the Len function.
It solves it for my code in VBA, but strangely not for my
code in the VB6 dll.
I have gone with this one now:

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

RBS


"Tom Ogilvy" wrote in message
...
Seems like you just need to add a check in your function for a valid

file
name. To the best of my knowledge, "" isn't a valid filename.

Apparently
Dir accepts it as a valid argument to return any file in the directory.

perhaps test

Function bFileExists3(ByVal sFile As String) As Boolean

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

End Function


--
Regards,
Tom Ogilvy


"RB Smissaert" wrote in message
...
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










All times are GMT +1. The time now is 09:02 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com