Visual Basic 6 Code


Thread Starter


How to use VB6 code to check for the existance of a specific file on the hard disk.

Mario Elmers

Here is some code for your problem

function filecheck(filename as string) as boolean

if dir(filename) <> "" then
filecheck = true
filecheck = false

end function

Mit freundlichen Grüßen / Best Regards
Mario Elmers

------------ Forwarded Message ------------
From: jbs7666
To: [email protected]
Subject: APPS: Visual Basic 6 Code

How to use VB6 code to check for the existance of a specific file on the hard


( Complete Thread )
If Dir(testfile) <> "" Then
'File exists
'File does not exist
End If

Set testfile equal to the name of the file you are looking for, IE -
testfile = "c:\test.dat"

Brian E Boothe

Here is Some Good Functions I use For These Tasks,
Brian E Boothe
Insoft Automation development
[email protected]

Option Explicit

' FUNCTION: FileExists
' Determines whether the specified File name exists.
' IN: [Path$] - name of File to check for(with full path)
' Returns: True if the File exists, False otherwise
Function FileExist(path$) As Integer
Dim x
x = FreeFile
On Error Resume Next
Open path$ For Input As x
FileExist = IIf(Err = 0, True, False)
Close x
Err = 0
End Function

' FUNCTION: DirExists
' Determines whether the specified directory name exists.
' IN: [strDirName] - name of directory to check for
' Returns: True if the directory exists, False otherwise
Public Function DirExists(ByVal strDirName As String) As Boolean
Const gstrNULL$ = ""
Dim strDummy As String

strDummy = Dir$(strDirName, vbDirectory)
If strDummy = gstrNULL$ Then
DirExists = False
DirExists = True
End If
End Function

'Function: To extract the path of the any file
' if input Filename="c:\windows\desktop\sample.txt"
' returns"c:\windows\desktop"
Public Function ExtractPath(Filename As String) As String
Dim l As Integer
Dim tempchar As String
l = Len(Filename)
While l > 0
tempchar = Mid(Filename, l, 1) 'trapping the last '\' char to
retrieve only the path of the setup file
If tempchar = "\" Then
ExtractPath = Mid(Filename, 1, l - 1)
Exit Function
End If
l = l - 1
End Function

'Function:- To find whether a particular type of file exist
' Process : for example here when we pass fileext as '.mdb' it checks
for access database files only
' when atleast one file exist of particular type returns true else
' set reference to Microsoft scripting run time under
' project-> Reference
Public Function SpecificFileExists(filepath As String, FileExt As
String) As Boolean

Dim fso As New Scripting.FileSystemObject
Dim folder As folder
Dim Filename As File
Dim path As String
path = filepath
If Len(Trim(path)) <> 0 Then
Set folder = fso.GetFolder(path) 'seting folder to variable for
easy manipulation
For Each Filename In folder.Files 'retrieving files one by one
If StrComp(Right(Filename.Name, 4), FileExt, vbTextCompare) = 0
SpecificFileExists = True
Exit Function
End If
End If
SpecificFileExists = False 'when no mdb is found in the folder
End Function
<p>Dear friend

<p>The existance of a file can be checked as follows:
if dir(<filepath>)<>"" then
file exists
file not exists
<p>the function dir() returns filename if file exists in the path.

<p>good luck.
' This is based on the file scripting object
' Pass the file string you are looking for as the
' filespec. If the file exists it will set
' blnExists to true

Sub ReportFileStatus(filespec)

Dim blnExists As Boolean
Dim fso

Set fso = CreateObject
blnExists = (fso.FileExists(filespec))

End Sub

<p>I've used this code everytime I need to know if there is any file which I need, but by this way you need to know in which directory is.

dim fich as string

'Here is the directory and the filename.
fich="c:\" & "filename.ext"

if dir(fich)<>"" then
' set of instructions if the file is.
' set of instructions if the file isn't
<p>I hope this code may be usefull for you.
<p>I hope this will help.

<p>You need to add a reference to the project. The reference should be Microsoft Scripting Runtime.
'Check that the File exists
Public Function CheckFileExists(strPathFile As String) As Boolean

'File System object in Microsoft scripting runtime
Dim fso As New FileSystemObject

Dim strPathFile As String

strPathFile = "C:\MyDirectory\Myfile"

'Check File exists
CheckFileExists = fso.FileExists(strPathFile)
End Function

David Alvarez Quiroga

I do not know exactly how it could be in VB6 but in VBA is like this:

Public Function FileExists(FileName As String) As Boolean
If Dir(FileName) = "" Then
FileExists = False
FileExists = True
End If
End Function
This example uses the Dir function to check if certain files and directories exist.

Dim MyFile
' Returns "WIN.INI" if it exists.
MyFile = Dir("C:\WINDOWS\WIN.INI")


yaser Ibrahim

I believe the command you are looking for is Dir.
Simply pass in the path and filename as a string parameter.

You can contact me at [email protected] if you need any further assistance.

Good Luck


I hope this will help. For more information use online help on Scripting.FileSystemObject

Public Sub TestFileExists()
Dim fso As Object
Dim pFileSpec As String

pFileSpec = "Your file path. example: c:\test\testfile.txt"

Set fso = CreateObject("Scripting.FileSystemObject")

If (fso.FileExists(pFileSpec)) Then
'Do some processing
End If

Set fso = Nothing

End Sub