如何判断一个文件夹是否存在

killvs 2004-05-11 07:18:39
如何判断一个文件夹是否存在dir只能判断一个文件的存在 那文件夹用什么函数?
...全文
263 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
BlueBeer 2004-05-12
  • 打赏
  • 举报
回复
如果是仅仅只要知道文件夹存不存在,dir好像也可以的
dir("c:\windows")返回空字符串,但
dir("c:\windows\")就可以返回c:\windows下的一个文件名了
用len(dir("c:\windows\"),返回0就说明不存在
也可能是个空文件夹?:)
这个办法不好:)

或者用getattr也行
得到文件夹属性就说明文件夹存在,出错就说明不存在
咳,这个办法和hisofty(瘦马)的思路一样

反正想知道一个文件夹存不存在的办法多了,我就不废话了:)
wumy_ld 2004-05-11
  • 打赏
  • 举报
回复
'以下代码判断一个文件夹是否存在,如果不存在,则创建它:
‘引用Microsoft Scripting Runtime
’创建一个指定的无限层文件夹
Public Function CreateDirectory(ByVal strDirectory As String) As Boolean
On Error GoTo ErrMsg
Dim Status
Dim fso As FileSystemObject
Dim strTempDir As String
Dim intPos As Integer

Screen.MousePointer = 11
CreateDirectory = False

’字符串是否为空
If strDirectory = "" Then GoTo ExitLab

’首先检查文件夹是否已经存在
Set fso = New FileSystemObject
If fso.FolderExists(strDirectory) = True Then
’已经存在
CreateDirectory = True
GoTo ExitLab
Else
If MsgBox("您输入的安装目录不存在,要创建该目录吗?", _
vbQuestion + vbYesNo + vbDefaultButton1, "询问") = vbNo Then GoTo ExitLab
End If

If Right(strDirectory, 1) <> "\" Then
strDirectory = strDirectory & "\"
End If

’不存在的情况
intPos = InStr(1, strDirectory, "\")
If intPos < 1 Then
MsgBox "您输入的文件夹不规范,请重新设置!", vbInformation, "提示"
GoTo ExitLab
End If

Do
strTempDir = Left(strDirectory, intPos - 1)
If fso.FolderExists(strTempDir) = False Then
fso.CreateFolder strTempDir
End If

intPos = InStr(intPos + 1, strDirectory, "\")
Loop Until intPos < 1

CreateDirectory = True

GoTo ExitLab
ErrMsg:
Status = SetError(Err.Number, Err.Description, Err.Source)
ErrMsg Status
ExitLab:
Screen.MousePointer = 0
End Function
roger_xiong 2004-05-11
  • 打赏
  • 举报
回复
点击“工具”-“引用”,引用 Microsoft Scripting runtime

Dim fso As New FileSystemObject
If fso.FolderExists("c:\temp") = False Then
fso.CreateFolder ("c:\temp")
End If

上述代码是指:如果"c:\temp"目录不存在的话就建立它。
hisofty 2004-05-11
  • 打赏
  • 举报
回复
Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
hisofty 2004-05-11
  • 打赏
  • 举报
回复
On Error Goto error
ChDir "H\Chowyunfat"
MsgBox "path In text1 exist's"
Exit Sub
error:
MsgBox "The path does Not exist"
yinweihong 2004-05-11
  • 打赏
  • 举报
回复
例:
引用Microsoft Script Control 1.0
Microsoft Scripting Runtime
Function CheckFolder(FoldrPath As String) As Boolean
Dim Exists As Boolean, CreateIt
Dim fso As New FileSystemObject
If (fso.FolderExists(FoldrPath)) Then
Exits = True
Else
Exits = False
'不存在则建立
Set CreateIt = fso.CreateFolder(FoldrPath)
End If
'值返回函数
CheckFolder = Exits
End Function
检验程式
Private Sub Form_Load()
Dim a As Boolean
a = CheckFolder("C:\新建文件夹")
MsgBox a
End Sub

7,763

社区成员

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

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