表格vb代码:依次判断文件 atxt、btxt 是否存在
以下是使用VB代码依次判断文件a.txt和b.txt是否存在的示例:
Sub CheckFilesExistence()
Dim filePaths As Variant
filePaths = Array("C:\path\to\a.txt", "C:\path\to\b.txt") ' 定义文件路径数组
Dim i As Integer
For i = LBound(filePaths) To UBound(filePaths) ' 遍历文件路径数组
If FileExists(filePaths(i)) Then ' 调用FileExists函数判断文件是否存在
MsgBox filePaths(i) & " exists." ' 文件存在时显示消息框
Else
MsgBox filePaths(i) & " does not exist." ' 文件不存在时显示消息框
End If
Next i
End Sub
Function FileExists(filePath As String) As Boolean
FileExists = Len(Dir(filePath)) > 0 ' 使用Dir函数判断文件是否存在
End Function
在上面的示例中,我们首先定义了一个名为CheckFilesExistence的子过程,其中声明了一个变量filePaths,并将包含文件a.txt和b.txt的路径存储在该变量中。然后,使用For循环遍历filePaths数组,对每个文件路径调用FileExists函数来判断文件是否存在。最后,根据文件是否存在,显示相应的消息框。
FileExists函数使用Dir函数来检查文件是否存在。如果Dir(filePath)返回的字符串长度大于0,则表示文件存在,否则表示文件不存在。
原文地址: https://www.cveoy.top/t/topic/jdfh 著作权归作者所有。请勿转载和采集!