VB 判断文件或文件夹是否存在

有时候需要判断文件或者文件夹是否存在,VB下使用dir函数 或者 使用FSO对象的object.FileExists(filespec) 方法 。

Public Function CheckName(ByVal tempName As String, Optional IsFolder As Boolean = False) As Boolean
    
    '#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~
    '函 数 名: CheckFileName
    '函数功能: 判断文件或文件夹是否存在,只对正常文件(文件夹)进行判断(不含隐藏及其他属性)。
    '函数参数: Lvw ,
    '返 回 值:
    '创建日期: 2012年10月12日
    '修改日期: 2012年10月13日11:17:41
    '#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~
    
    On Error GoTo hErr
    
    Dim t As Boolean
    
    '判断长度是否为空
    If Len(tempName) = 0 Then
    
        Exit Function
    End If
    
    '判断是否含有 ":"
    If InStr(1, tempName, ":", vbBinaryCompare) = 0 Then
    
        Exit Function
    End If
    
    '文件判断或文件夹判断?
    If IsFolder = True Then
    
        If Len(Dir(tempName, vbDirectory)) > 0 Then
        
            t = True
        Else
        
            t = False
        End If
    Else
    
        If Len(Dir(tempName)) > 0 Then
        
            t = True
        Else
        
            t = False
        End If
    End If
    
    CheckName = t
    
    Exit Function
    
hErr:
    CheckName = False
End Function