我试图从文件夹中获取某些文件的日期和时间(上次修改) . 我设法获得日期和小时/分钟/秒,但我无法得到 milliseconds .
我已经尝试过尽可能地格式化列 . 我只得到 0
毫秒 .
我的代码到目前为止:
-
用户选择一个文件夹
-
代码在A列中显示找到的所有文件名,在B列中显示日期,小时,分钟和秒(最后修改日期/时间)
我该怎么做才能获得当前代码以获得毫秒数?
这是我的代码:
Private Function GetAllFiles(ByVal strPath As String, _
ByVal intRow As Integer, ByRef objFSO As Object) As Integer
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
i = intRow - ROW_FIRST + 1
Set objFolder = objFSO.GetFolder(strPath)
For Each objFile In objFolder.Files
'print file name
Cells(i + ROW_FIRST + 2, 1) = objFile.Name
'print file path
Cells(i + ROW_FIRST + 2, 2) = objFile.DateLastModified
i = i + 1
Next objFile
GetAllFiles = i + ROW_FIRST - 1
End Function
1 回答
以下模块将使用Windows API调用检索Windows文件创建,修改或访问日期时间(包括毫秒) .
但必须指出的是,存在许多潜在问题 . 一个很大的问题是VBA
Date
数据类型的分辨率为1秒,因此日期时间需要作为String返回,或者存储在不同的数据类型中(Currency
是正确的大小 . )我完美但它的工作原理可以根据您的个人需求进行调整 . 请注意,您需要手动取消注释希望函数返回的日期时间的行 .
在使用它之前,请务必阅读以获取重要信息,因为根据文件系统的不同,存在一些限制 . 例如,NTFS通常会在您“认为”完成后完成写入文件...最多1小时后 .
更多信息:
VB论坛:Code Source(注意作者的错误在他的下一篇文章中提到过 . )
MSDN:Windows File Times
MSDN:GetFileTime Function(Windows / C)
Stack Overflow:VBA String with Milliseconds to Date