BobbyKearan
BobbyKearan

Reputation: 41

GetLastWriteTime not accurate

I'm using a function to check on timestamps on files on remote computers. We have a LOT of computers.

What I am running into is a bit odd.

I run the query on a lot of computers. Time stamp on Computer26887 comes up as "1/4/2013 12:46:01 AM" I UNC to that computer and check the file. In explorer, timestamp says "9/16/2013 11:23 AM" (an hour and a half ago) Hmmm.... Query again - same old, wrong, timestamp. I right-click on the file, go to properties and click "Details" in the properties window - "Date Modified 9/16/2013 11:23 AM" Query again with the vb code - now it shows the right timestamp????

I've got hundreds of systems to go through and if I can't trust the data I'm getting, I have way too much work to do!!!

Any ideas?

Update
Basically, VB.NET is retrieving a cached version of the timestamp. The timestamp has been updated, but the cache still has the old timestamp. How do I force the cache to update without manually opening the properties of the file in explorer??

Shared Function GetFileInfo(ByVal ComputerName As String, ByVal FiletoFind As String, info As String)
    Dim Ret As String = ""
    Dim targetfile = "\\" & ComputerName & "\" & FiletoFind
    Dim fi As FileInfo = New FileInfo(targetfile)
    If fi.Exists Then
        fi.refresh
        Select Case info
            Case Is = "Exists"
                Ret = fi.Exists.ToString
            Case Is = "Delete"
                fi.Delete()
                Ret = fi.Exists.ToString
            Case Is = "Created"
                Ret = fi.CreationTime.ToString("MM/dd/yyyy hh:mm:ss tt")
            Case Is = "Access"
                Ret = fi.LastAccessTime.ToString("MM/dd/yyyy hh:mm:ss tt")
            Case Is = "Mod"
                Ret = fi.LastWriteTime.ToString("MM/dd/yyyy hh:mm:ss tt")
        End Select
    Else
        Ret = "File Not Found"
    End If

    Ret = Ret.Replace(vbCrLf, "")
    Ret = Ret.Replace(vbCr, "")

    Return Ret

End Function

(I've also tried using File rather than FileInfo... check the post history)

UPDATE
As a test, I did a file check on a system using AutoIT3 code. It returned accurate information. After the AutoIT3 check, vb.net returned accurate timestamps. So, what is the problem with vb.net that AutoIT3 does a better job??

Func _timestampchk($path)
Dim $file,$astamp
$file = $path
$astamp = FileGetTime($file, 0, 0)
If IsArray($astamp) Then
    $stamp = $astamp[1] & "/" & $astamp[2] & "/" & $astamp[0] & " " & $astamp[3] & ":" & $astamp[4]
ElseIf $astamp = 0 Then
    $stamp = "File " & $path & " not Found"
Else
    $stamp = 0
EndIf
Return $stamp
EndFunc   ;==>_timestampchk

Upvotes: 4

Views: 2101

Answers (1)

Tony
Tony

Reputation: 17637

Try FileSystem.GetFileInfo , as pointed by Andrew Morton, and neoistheone

Dim information = My.Computer.FileSystem.GetFileInfo("C:\MyLogFile.log")
information.Refresh()   ' Calls must be made to Refresh before attempting to get the attribute information, or the information will be outdated.
MsgBox("The file's full name is " & information.FullName & ".")
MsgBox("Last access time is " & information.LastAccessTime & ".")
MsgBox("The length is " & information.Length & ".")

Although documented, and rationale, it is a weird behavior. As curiosity, I would also try these code below to see if the problem also happens. Try to use Windows API and see if it stills return the 'cached' data.

Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile _ 
As Long, lpCreationTime As FILETIME, lpLastAccessTime _ 
As FILETIME, lpLastWriteTime As FILETIME) As Long

If this don´t solve, try to call the Open Properties window programatically, then read the date time information, and see if it solves the problem:

'#VBIDEUtils#***********************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : [email protected]
' * Date             : 28/06/99
' * Time             : 13:16
' ********************************************************
' * Comments         : Showing the Properties dialog box
' *
' * This tip demonstrates how To use the Win32 API To
' * bring up the explorer properties
' * dialog box For a specified file.
' * This API Function has the same effect As Right
' * clicking On a file In Windows 95 And selecting properties.

' **************************************************
Option Explicit On

    Private Type SHELLEXECUTEINFO
        cbSize As Long
        fMask As Long
        hwnd As Long
        lpVerb As String
        lpFile As String
        lpParameters As String
        lpDirectory As String
        nShow As Long
        hInstApp As Long
        lpIDList As Long
        lpClass As String
        hkeyClass As Long
        dwHotKey As Long
        hIcon As Long
        hProcess As Long
    End Type

Private Const SEE_MASK_INVOKEIDLIST = &HC
Private Const SEE_MASK_NOCLOSEPROCESS = &H40
Private Const SEE_MASK_FLAG_NO_UI = &H400

Private Declare Function ShellExecuteEX Lib "shell32.dll" Alias _
   "ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long

Public Function ShowProps(FileName As String, _
   OwnerhWnd As Long) As Boolean

    'USAGE:
    'To show the properties dialog box of "c:\autoexec.bat", use the following code:
    'Call ShowProps("c:\autoexec.bat", Me.hwnd)
    'Function will return false if
    'property windows can't be shown for
    'any reason (e.g., invalid file or Ownerhwnd)


    On Error Resume Next
    Dim SEI As SHELLEXECUTEINFO
    Dim r As Long
    With SEI
        .cbSize = Len(SEI)
        .fMask = SEE_MASK_NOCLOSEPROCESS Or _
         SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
        .hwnd = OwnerhWnd
        .lpVerb = "properties"
        .lpFile = FileName
        .lpParameters = vbNullChar
        .lpDirectory = vbNullChar
        .nShow = 0
        .hInstApp = 0
        .lpIDList = 0
    End With
    r = ShellExecuteEX(SEI)
    ShowProps = r
End Function

Upvotes: 1

Related Questions