Visual Basic 6 Code

J

Thread Starter

jbs7666

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

Mario Elmers

Here is some code for your problem

function filecheck(filename as string) as boolean

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

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
disk.

Thanks

( Complete Thread http://www.control.com/1026181511/index_html )
 
If Dir(testfile) <> "" Then
'File exists
Else
'File does not exist
End If

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

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
Else
DirExists = True
End If
End Function

'-----------------------------------------------------------------------
----------
'Function: To extract the path of the any file
'Process:
' 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
Wend
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
false
' 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
Then
SpecificFileExists = True
Exit Function
End If
Next
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:
<pre>
if dir(<filepath>)<>"" then
file exists
else
file not exists
endif
</pre>
<p>the function dir() returns filename if file exists in the path.

<p>good luck.
 
B
<pre>
' 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
("Scripting.FileSystemObject")
blnExists = (fso.FileExists(filespec))

End Sub
</pre>
 
<p>Hello!

<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.

<pre>
'CODE
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.
else
' set of instructions if the file isn't
endif
'END of CODE
</pre>
<p>I hope this code may be usefull for you.
 
B
<p>I hope this will help.

<p>You need to add a reference to the project. The reference should be Microsoft Scripting Runtime.
<pre>
'************************************
'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
</pre>
 
D

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
Else
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")

 
Y

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

-Yaser

 
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
 
Top