如果指定的檔案存在,則傳回 True;如果不存在,則傳回 False。
語法
object.FileExists(filespec)
引數:
FileSystemObject 的名稱。下列範例說明 FileExists 方法的用法。
範例 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.
範例 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.