【转】检测文件或文件夹是否存在的两个函数-金宇
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


【转】检测文件或文件夹是否存在的两个函数

发表时间:2014/1/28 8:45:47 评论(1) 浏览(9309)  评论 | 加入收藏 | 复制
   
摘 要:检测文件或文件夹是否存在的两个函数
正 文:

Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
    'Purpose:   Return True if the file exists, even if it is hidden.
    'Arguments: strFile: File name to look for. Current directory searched if no path included.
    '           bFindFolders. If strFile is a folder, FileExists() returns False unless this argument is True.
    'Note:      Does not look inside subdirectories for the file.
    'Author:    Allen Browne. http://allenbrowne.com June, 2006.

    Dim lngAttributes As Long

    'Include read-only files, hidden files, system files.

    lngAttributes = (vbReadOnly or vbHidden or vbSystem)

    If bFindFolders Then
        lngAttributes = (lngAttributes or vbDirectory) 'Include folders as well.
    Else
        'Strip any trailing slash, so Dir does not look inside the folder.

        Do While Right$(strFile, 1) = "\"
            strFile = Left$(strFile, Len(strFile) - 1)
        Loop
    End If

    'If Dir() returns something, the file exists.

    On Error Resume Next
    FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function

Function FolderExists(strPath As String) As Boolean
    On Error Resume Next
    FolderExists = ((GetAttr(strPath) And vbDirectory) = vbDirectory)
End Function

调用示例:

    MsgBox FileExists("Y:\供应商附件\测试.txt")
    MsgBox FolderExists("Y:\供应商附件")


Access软件网交流QQ群(群号:198465573)
 
 相关文章
用代码检查指定文件是否存在  【cuxun  2007/10/22】
判断指定文件是否存在  【ExcelHome  2008/10/27】
[示例]函数检测在表中是否存在某字段  【钱玉炜  2009/5/20】
【译文】判断数据库中是否存在某个查询  【周芳  2012/5/14】
判断数组中是否存在输入值  【宏鹏  2013/1/23】
检测网站目录下的某个文件是否存在的函数  【金宇  2013/2/25】
sqlserver中判断表或临时表是否存在  【平常心  2013/7/22】
判断MS SQLSERVER临时表是否存在  【smeyou  2013/9/30】
常见问答
技术分类
相关资源
文章搜索
关于作者

金宇

文章分类

文章存档

友情链接