如何判断文件夹是否存在

mjpclab 2002-11-16 03:05:56
如何判断文件夹是否存在?用dir()的缺点是如果文件夹里没有文件,则也会返回空值.
...全文
26 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccbl 2002-11-16
  • 打赏
  • 举报
回复
fso对象和DIR()函数
mjpclab 2002-11-16
  • 打赏
  • 举报
回复
不加"\"也没用啊,给出代码看看……

最好不要引用外部文件,否则附带文件太麻烦
sea 2002-11-16
  • 打赏
  • 举报
回复
加入microsoft scripting runtime(scrrun.dll)引用
Dim obj As New FileSystemObject
If obj.FolderExists("c:\temp") Then
'目录存在
End If
Set obj = Nothing
用以上代码判断即可
superhard 2002-11-16
  • 打赏
  • 举报
回复
dir()如果找到不会返回空
zyl910 2002-11-16
  • 打赏
  • 举报
回复
不要加“\”
那表示是一个路径,而不是一个目录(文件夹)
(“C:\”那样的根目录除外)
faib920 2002-11-16
  • 打赏
  • 举报
回复
Private Sub Form_Load()
Dim f As String
f = Dir("c:\temp", vbDirectory)
If f <> "" Then MsgBox "c:\temp存在", , ""
If f = "" Then MsgBox "c:\temp不存在", , ""
End Sub
w18ily 2002-11-16
  • 打赏
  • 举报
回复

'The file you want to check
Const strFile = "C:\testfile.txt"

Private Sub Form_Load()
Dim msg As String
If ReportFileStatus(strFile) Then
msg = strFile & " exists."
Else
msg = strFile & " doesn't exist."
End If
MsgBox msg
End Sub
'The Method of FSO
'Add the Reference of Misrosoft Scripting Runtime
Function ReportFileStatus(filespec As String) As Boolean

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
ReportFileStatus = True
Else
ReportFileStatus = False
End If
End Function
'The Method of Dir
Function ReportFileStatus(filespec As String) As Boolean
Dim strdir As String
strdir = Dir(filespec)
If strdir <> "" Then
ReportFileStatus = True
Else
ReportFileStatus = False
End If
End Function

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧