Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check to see if a file exists 2

Status
Not open for further replies.

Cimso

Technical User
Sep 25, 2001
22
US
How would I write an IF EXIST type statment? I'm need to check if a file exists.
 
You can also use the FileSystemObject Object

Set fs = CreateObject("Scripting.FileSystemObject")
If fs.fileexists("filename.txt") Then

'Insert Code Here

EndIf

David Moore
dm7941@sbc.com
 
FSO is overkill in this situation. VB has a built-in function Dir(), which returns the filename if the file exists, or "" if it doesn't. i.e.

If Dir(&quot;C:\Autoexec.bat) <> &quot;&quot; Then ...
 
Here's how I do it:

---------------------
Public Function FileExists(File As String) As Boolean
On Error Resume Next
FileExists = (Dir$(File) <> &quot;&quot;)
On Error Goto 0
End Function

---------------------

Hope that helps!

~Mike

Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top