VB 判断当前目录文件是否存在?

2025-03-23 10:31:59
推荐回答(5个)
回答1:

这可用下列语句进行判断 

Dir([pathname],[Attributes as VbFileAttribute=vbNormal]) As String 

解释: pathname:文件的绝对路径; 

Attributes:文件的属性。 

“[]”内为可选项。dir(file)=""表示文件不存在,dir(file)<>""表示文件存在。 

例如判断c:wpswinwps.exe是否存在,如存在,就调用它,可用下列语句: 

if dir("c:wpswinwps.exe")<>"" then 

shell "c:wpswinwps.exe" 

end if 

回答2:

if dir(app.path & "\abc.txt")="" then
'不存在
else
'存在
end if

回答3:

if dir(app.path & "\abc.txt")<>"" then
MsgBox app.path & "\abc.txt")<>"" & "文件存在 "
else
MsgBox app.path & "\abc.txt")<>"" & "文件不存在 "
end if

回答4:

dir(你的文件路径)>0 表示文件存在,否则不存在

回答5:

Dim fsoTest As New FileSystemObject
If fsoTest.FolderExists(App.Path & "\abc.txt") = True Then
MsgBox "文件存在 "
Else
MsgBox "不存在 "
End If