How Can I Find Out If a File Exists On a Disk?

To find out if a file exists on a disk you can use the Visual Basic Dir() function. It has the following syntax:

Dir[(pathname[, attributes])]

In this context, pathname would be the name of the file that you are trying to find. The Attributes can be one or more of the following values (separated by the OR operator).

Attribute Description
vbNormal Default Attribute
vbReadOnly Use if file pathname is read-only
vbHidden Use if the file pathname is hidden
vbSystem Use if the file pathname is a system file
vbArchive Use if the file pathname is an Archive file
vbDirectory Use if pathname is a directory

Example:

If Dir(myfilename, vbNormal or vbReadOnly or vbHidden or vbSystem or vbArchive) = "" then
Call Msgbox ("This file does not exist")
Else
Call msgbox ("This file does exist")
End If

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: