Office中国论坛/Access中国论坛

标题: 如何取得外部文件的日期 [打印本页]

作者: yaozi    时间: 2009-12-12 16:51
标题: 如何取得外部文件的日期
本帖最后由 yaozi 于 2009-12-17 08:54 编辑

请教一下,如何取得外部文件(不是数据库本身)的创建日期或修改日期,谢谢。
作者: chaojianan    时间: 2009-12-12 17:20
Option Compare Database

'»ñÈ¡ÎļþÐÅÏ¢µÄGetFileInfoº¯Êý
Type FileInfo
    FileName As String   'Ãû×Ö
    ShortName As String
    Type As String    'ÀàÐÍ
        Size As Long
        DateCreate As Date
        DateLastModified As Date '
        DateLastAccessed As Date
        Attributes As String
    End Type

    '===============================================================================
    '-º¯ÊýÃû³Æ:     GetFileInfo
    '-¹¦ÄÜÃèÊö:     »ñÈ¡ÎļþÐÅÏ¢
    '-ÊäÈë²ÎÊý˵Ã÷: ²ÎÊý1: ±ØÑ¡ strFile As String Îļþ·¾¶ºÍÃû³Æ
    '-·µ»Ø²ÎÊý˵Ã÷: ·µ»ØÎļþÃû³Æ£¬ÀàÐÍ£¬´óС£¬¸üÐÂÈÕÆÚµÈ
    '-ʹÓÃÓ﷨ʾÀý: Msgbox GetFileInfo("C:\Abc.txt").Size
    '-ÒýÓÃ:Microsoft Scripting Runtime
    '===============================================================================
Function GetFileInfo(strFile As String) As FileInfo
    On Error Resume Next
    Dim fsoSys As New Scripting.FileSystemObject
    Dim fsoFile As File
    Dim strstrastr As String

    Set fsoFile = fsoSys.GetFile(strFile)
    With GetFileInfo
        .FileName = fsoFile.Name
        .ShortName = fsoFile.ShortName
        .Type = fsoFile.Type
        .Size = fsoFile.Size / 1000
        .DateCreate = fsoFile.DateCreated
        .DateLastModified = fsoFile.DateLastModified
        .DateLastAccessed = fsoFile.DateLastAccessed

        If fsoFile.Attributes And Archive Then
            strAstr = strAstr & "³£¹æ "
        End If
        If fsoFile.Attributes And ReadOnly Then
            strAstr = strAstr & "Ö»¶Á "
        End If
        If fsoFile.Attributes And Hidden Then
            strAstr = strAstr & "Òþ²Ø "
        End If
'        If fsoFile.Attributes And System Then 'ÕâÒ»¾äÓÐÎÊÌ⣬Ìáʾsystem䶨Òå
        If fsoFile.Attributes And 4 Then
            strAstr = strAstr & "ϵͳ "
        End If
        If fsoFile.Attributes And Compressed Then
            strAstr = strAstr & "ѹËõ "
        End If
        .Attributes = strAstr
    End With

    Set fsoSys = Nothing
    Set fsoFile = Nothing

End Function
作者: chaojianan    时间: 2009-12-12 17:21
不好意思,刚才的代码有乱码,看看这个。
Option Compare Database

'获取文件信息的GetFileInfo函数
Type FileInfo
    FileName As String   '名字
    ShortName As String
    Type As String    '类型
        Size As Long
        DateCreate As Date
        DateLastModified As Date '
        DateLastAccessed As Date
        Attributes As String
    End Type

    '===============================================================================
    '-函数名称:     GetFileInfo
    '-功能描述:     获取文件信息
    '-输入参数说明: 参数1: 必选 strFile As String 文件路径和名称
    '-返回参数说明: 返回文件名称,类型,大小,更新日期等
    '-使用语法示例: Msgbox GetFileInfo("C:\Abc.txt").Size
    '-引用:Microsoft Scripting Runtime
    '===============================================================================
Function GetFileInfo(strFile As String) As FileInfo
    On Error Resume Next
    Dim fsoSys As New Scripting.FileSystemObject
    Dim fsoFile As File
    Dim strstrastr As String

    Set fsoFile = fsoSys.GetFile(strFile)
    With GetFileInfo
        .FileName = fsoFile.Name
        .ShortName = fsoFile.ShortName
        .Type = fsoFile.Type
        .Size = fsoFile.Size / 1000
        .DateCreate = fsoFile.DateCreated
        .DateLastModified = fsoFile.DateLastModified
        .DateLastAccessed = fsoFile.DateLastAccessed

        If fsoFile.Attributes And Archive Then
            strAstr = strAstr & "常规 "
        End If
        If fsoFile.Attributes And ReadOnly Then
            strAstr = strAstr & "只读 "
        End If
        If fsoFile.Attributes And Hidden Then
            strAstr = strAstr & "隐藏 "
        End If
'        If fsoFile.Attributes And System Then '这一句有问题,提示system未定义
        If fsoFile.Attributes And 4 Then
            strAstr = strAstr & "系统 "
        End If
        If fsoFile.Attributes And Compressed Then
            strAstr = strAstr & "压缩 "
        End If
        .Attributes = strAstr
    End With

    Set fsoSys = Nothing
    Set fsoFile = Nothing

End Function
作者: chaojianan    时间: 2009-12-12 17:21
Option Compare Database

'»ñÈ¡ÎļþÐÅÏ¢µÄGetFileInfoº¯Êý
Type FileInfo
    FileName As String   'Ãû×Ö
    ShortName As String
    Type As String    'ÀàÐÍ
        Size As Long
        DateCreate As Date
        DateLastModified As Date '
        DateLastAccessed As Date
        Attributes As String
    End Type

    '===============================================================================
    '-º¯ÊýÃû³Æ:     GetFileInfo
    '-¹¦ÄÜÃèÊö:     »ñÈ¡ÎļþÐÅÏ¢
    '-ÊäÈë²ÎÊý˵Ã÷: ²ÎÊý1: ±ØÑ¡ strFile As String Îļþ·¾¶ºÍÃû³Æ
    '-·µ»Ø²ÎÊý˵Ã÷: ·µ»ØÎļþÃû³Æ£¬ÀàÐÍ£¬´óС£¬¸üÐÂÈÕÆÚµÈ
    '-ʹÓÃÓ﷨ʾÀý: Msgbox GetFileInfo("C:\Abc.txt").Size
    '-ÒýÓÃ:Microsoft Scripting Runtime
    '===============================================================================
Function GetFileInfo(strFile As String) As FileInfo
    On Error Resume Next
    Dim fsoSys As New Scripting.FileSystemObject
    Dim fsoFile As File
    Dim strstrastr As String

    Set fsoFile = fsoSys.GetFile(strFile)
    With GetFileInfo
        .FileName = fsoFile.Name
        .ShortName = fsoFile.ShortName
        .Type = fsoFile.Type
        .Size = fsoFile.Size / 1000
        .DateCreate = fsoFile.DateCreated
        .DateLastModified = fsoFile.DateLastModified
        .DateLastAccessed = fsoFile.DateLastAccessed

        If fsoFile.Attributes And Archive Then
            strAstr = strAstr & "³£¹æ "
        End If
        If fsoFile.Attributes And ReadOnly Then
            strAstr = strAstr & "Ö»¶Á "
        End If
        If fsoFile.Attributes And Hidden Then
            strAstr = strAstr & "Òþ²Ø "
        End If
'        If fsoFile.Attributes And System Then 'ÕâÒ»¾äÓÐÎÊÌ⣬Ìáʾsystem䶨Òå
        If fsoFile.Attributes And 4 Then
            strAstr = strAstr & "ϵͳ "
        End If
        If fsoFile.Attributes And Compressed Then
            strAstr = strAstr & "ѹËõ "
        End If
        .Attributes = strAstr
    End With

    Set fsoSys = Nothing
    Set fsoFile = Nothing

End Function
作者: yaozi    时间: 2009-12-17 08:35
好像不行吧,只出现一些数字。
作者: yaozi    时间: 2009-12-17 08:52
不好意思,刚才的代码有乱码,看看这个。
Option Compare Database

'获取文件信息的GetFileInfo函数
Type FileInfo
    FileName As String   '名字
    ShortName As String
    Type As String    '类型
...
chaojianan 发表于 2009-12-12 17:21

谢谢,成功了。刚才弄错了




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3