Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Check if A File exists

0 views
Skip to first unread message

Joe

unread,
Aug 15, 2001, 11:12:18 AM8/15/01
to
Greetings,

Sorry if this question seems extremely basic (no punn intended) but I've
been reading 2 books on Visual Basic 6 and although they give good insight
into how fileio works with Visual Basic 6, they never really mention how to
check for existing files. They simply state that if the file exists then Vb
will do "blah blah" or if it doesn't then it will create it, depending on
the mode (random, binary etc.)

Does anyone know a good way to check if a file already exists? I would like
to set a varible = 1 if the file already exists so I can use that info later
in the program.

Thanks,

Joe


Hugh Laderman

unread,
Aug 15, 2001, 11:18:51 AM8/15/01
to
Here's one way (I believe based on a J French function, but with better
variable names <g>)

Public Function FileExists(ByVal FileName As String) As Boolean
'
Dim Attr As VbFileAttribute

On Error Resume Next
Attr = GetAttr(FileName)
If Err.Number <> 0 Then
FileExists = False
ElseIf (Attr And vbDirectory) Then
FileExists = False 'is directory
Else
FileExists = True 'exists, not a directory
End If
Err.Clear
On Error GoTo 0

End Function

"Joe" <dgtala...@hotmail.com> wrote in message
news:mjwe7.39096$7G.4...@typhoon.mw.mediaone.net...

Steve Szudzik

unread,
Aug 15, 2001, 2:30:08 PM8/15/01
to
Public Function FileExists(szFileName) As Boolean
Dim oFile As New FileSystemObject

FileExists = oFile.FileExists(szFileName)

Set oFile = Nothing
End Function

Steve Szudzik
http://www.szudzikfamily.com


"Joe" <dgtala...@hotmail.com> wrote in message
news:mjwe7.39096$7G.4...@typhoon.mw.mediaone.net...

BD

unread,
Aug 15, 2001, 2:43:06 PM8/15/01
to
Try this way:

Dim intFileExist as integer
Dim strFileName as string

strFileName=dir("c:\path\filename")
if strFileName <>"" then
intFileExist = 1
else
intFileExist = 0
endif


Regards
BD


On Wed, 15 Aug 2001 15:12:18 GMT, "Joe" <dgtala...@hotmail.com>
wrote:

J French

unread,
Aug 15, 2001, 3:07:23 PM8/15/01
to
Please don't suggest such things - it is cruel to entice people to
build bear traps for themselves.

*Never* use Dir in a 'core utility' routine - it interferes with other
Dir loops.

The FindFirstFile/FindNextFile API method is safe - but personally I
have been using (variations) of the GetAttr method since 1987 and have
found it absolutely fine.

As for using the FSO ... that really is a sledgehammer to crack a
hazelnut.

On Wed, 15 Aug 2001 19:43:06 +0100, BD <bd_jlr...@hotmail.com>
wrote:

Joe

unread,
Aug 16, 2001, 1:45:57 PM8/16/01
to
Greetings,

Just wanted to thank you all. That was very helpful.

Joe


"Joe" <dgtala...@hotmail.com> wrote in message
news:mjwe7.39096$7G.4...@typhoon.mw.mediaone.net...

0 new messages