获取带有MILLISECONDS的文件的最后修改日期? [英] Obtain the last modified date of a file with MILLISECONDS?
问题描述
我正在尝试从文件夹中获取某些文件的日期和时间(最后修改).我设法获取了日期和小时/分钟/秒,但没有得到 毫秒 .
I am trying to get the date and time (last modified) of some files from a folder. I managed to get the date and hour/minutes/seconds but I can not get the milliseconds.
我已经尝试过以所有可能的方式格式化该列.我只得到0
毫秒.
I already tried formatting the column in all the ways possible. I only get 0
for milliseconds.
到目前为止,我的代码可以:
My code so far does:
-
用户选择一个文件夹
the user chooses a folder
代码在A列中显示找到的所有文件名,在B列中显示日期,小时,分钟和秒(最后修改的日期/时间)
the code displays in column A all the file names found and in column B the date, hour,minute and seconds (last modified date/time)
我应该对当前代码执行什么操作才能获得毫秒数?
What should I do to the current code to obtain the milliseconds ?
这是我的代码:
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
推荐答案
以下模块将使用Windows API调用来检索Windows文件的创建,修改或访问的日期时间(包括毫秒).
The following module will retrieve Windows file creation, modify or accessed datetime including milliseconds, using a Windows API call.
但是,必须指出的是,存在许多潜在的问题.一个很大的问题是VBA Date
数据类型的分辨率为1秒,因此日期时间需要以String的形式返回或存储在其他数据类型中(Currency
是正确的大小.)
However it must be noted that there are a number of potential issues. A big one is that the VBA Date
data type has a resolution of 1 second, so the datetime needs to be returned as a String, or stored in a different data type (Currency
is the correct size.)
Option Explicit
Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile As Long, _
lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, _
lpLastWriteTime As FILETIME) As Long
Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, lpSecurityAttributes As Any, _
ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Declare Function FileTimeToSystemTime Lib "kernel32.dll" _
(lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_ALWAYS = 2
Const CREATE_NEW = 1
Const OPEN_ALWAYS = 4
Const OPEN_EXISTING = 3
Const TRUNCATE_EXISTING = 5
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000
Const FILE_FLAG_NO_BUFFERING = &H20000000
Const FILE_FLAG_OVERLAPPED = &H40000000
Const FILE_FLAG_POSIX_SEMANTICS = &H1000000
Const FILE_FLAG_RANDOM_ACCESS = &H10000000
Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
Const FILE_FLAG_WRITE_THROUGH = &H80000000
Function GetDateValue(fName As String) As String
'returns UTC (GMT) file time for specified file
Dim hFile As Long ' handle to the opened file
Dim ctime As FILETIME ' receives time of creation
Dim atime As FILETIME ' receives time of last access
Dim mtime As FILETIME ' receives time of last modification
Dim Thetime As SYSTEMTIME ' used to manipulate the time
Dim retval As Long ' return value
hFile = CreateFile(fName, GENERIC_READ, FILE_SHARE_READ, _
ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
retval = GetFileTime(hFile, ctime, atime, mtime)
'Choose which date to return: creation, modify or access date
'retval = FileTimeToSystemTime(ctime, Thetime) 'extract creation datetime
retval = FileTimeToSystemTime(mtime, Thetime) 'extract modified datetime
'retval = FileTimeToSystemTime(atime, Thetime) 'extract accessed datetime
retval = CloseHandle(hFile)
With Thetime
GetDateValue = .wYear & Format(.wMonth, "\-00") & _
Format(.wDay, "\-00") & " " & Format(.wHour, "00") & _
Format(.wMinute, "\:00") & Format(.wSecond, "\:00") & _
Format(.wSecond, "\.000")
End With
End Function
Sub test()
MsgBox GetDateValue("c:\logfile.txt")
'returns a string like "2018-03-31 16:13:52.052"
End Sub
我只是在这里粘贴它,虽然它不是完美的,但它可以工作并且可以根据您的个人需求进行调整.请注意,您需要为要返回该函数的日期时间 手动取消注释该行.
I'm just pasting this here, it isn't perfect but it works and can be adjusted to your individual needs. Note that you need to manually uncomment the line for which datetime you want the function to return.
在使用前,请务必先阅读重要内容,因为根据文件系统等的不同,会有一些限制.例如,在您认为"文件完成后,NTFS通常会在一个小时后完成写文件.
Be sure to read up before you use this for anything important because there are limitations depending on your file system and more. For example, NTFS will often finish writing a file after you "think" it's finished... up to 1 hour later.
-
VB论坛:代码来源(请注意作者在随后的帖子中提到的错误.)
VB Forumus: Code Source (note the author's error mentioned in his following post.)
MSDN: Windows文件时间
MSDN: GetFileTime函数(Windows/C ++)
MSDN : GetFileTime Function (Windows/C++)
堆栈溢出: VBA字符串(以毫秒为单位)
这篇关于获取带有MILLISECONDS的文件的最后修改日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!