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.
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
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 ?
This is my code:
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
The following module will retrieve Windows file creation, modify or accessed datetime including milliseconds, using a Windows API call.
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.)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.
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.
More Information:
VB Forumus: Code Source (note the author's error mentioned in his following post.)
MSDN : Windows File Times
MSDN : GetFileTime Function (Windows/C++)
Stack Overflow : VBA String with Milliseconds to Date