Thursday, July 28, 2011

VB6 - How to check if a file exist?

There are cases in making a program that we need to check if a file is existing or not. This post will give you an idea or tips on how to check if a file does exist or not in Visual Basic 6.0.

First, you need to go to the "Projects Menu" then click on "References". Look for the reference "Microsoft Scripting Runtime" and put a check mark on it and press the button "OK" to add the reference.



After doing that, we now add our code. Insert this code below anywhere in your program. In my case I choose to put it in a module so that I can access it anytime in every form of my project.

Public Function FileExists(sFullPath As String) As Boolean
Dim oFile As New Scripting.FileSystemObject
FileExists = oFile.FileExists(sFullPath)
End Function


Now you can call on the function like this:

If FileExists("C:\temp\try.txt") Then
Msgbox "File exist."
End If


I hope this snippet has helped you in any way it can. Enjoy!

First

This is the first post for this website and will definitely won't be the last. This website is designed for those programmers having a hard time at particular cases in their coding. The site will offer useful code snippets for those programmers that addresses every post that this site will make.

Have a nice day and keep coding.