FileExists Method
Returns True if a specified file exists; False if it does not.
Syntax
object.FileExists(filespec)
Arguments:
- Object: Required. Always the name of a
FileSystemObject
. - Filespec: Required. An absolute path with file whose existence is to be determined.
The following example illustrates the use of the FileExists
method.
Example 1:
Function CheckFileExists(filespec)
Dim fso, msg
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
msg = filespec & " exists."
Else
msg = filespec & " doesn't exist."
End If
CheckFileExists = msg
End Function
' Sample usage
Dim result
result = CheckFileExists("C:\example.txt")
' Outputs: C:\example.txt exists. or C:\example.txt doesn't exist.
Example 2:
Function CheckMultipleFiles(files)
Dim fso, file, result
Set fso = CreateObject("Scripting.FileSystemObject")
For Each file In files
If fso.FileExists(file) Then
result = result & file & " exists."
Else
result = result & file & " doesn't exist."
End If
Next
CheckMultipleFiles = result
End Function
' Sample usage
Dim filesToCheck, checkResult
filesToCheck = Array("C:\file1.txt", "C:\file2.txt", "D:\file3.txt")
checkResult = CheckMultipleFiles(filesToCheck)
' Outputs the existence status of each file in the array.